Yii框架官方指南系列增補版39-測驗:單元測驗(Unit Testing)

黄舟
發布: 2023-03-05 18:38:01
原創
1266 人瀏覽過



因為Yii測試框架基於PHPUnit構建,所以推薦你在理解如何寫一個單元測試之前先通讀一遍PHPUnit文檔。例如我們簡單概括一下在Yii中寫一個單元測試的基本原則:

  • 一個單元測試以繼承自CTestCase或者CDbTestCase的XyzTest類的形式編寫, 其中Xyz代表要被測試的類., 想要測試的類. Post類別,我們會相應地將測試類別命名為PostTest. 基底類別CTestCase是通用單元測試類別, 而CDbTestCase只適用於測試AR模型類別. 由於PHPUnit_Framework_TestCase是這兩個類別的父類別, 我們可以從這個類別中繼承所有方法。

  • 單元測試類別以XyzTest.php的形式保存在PHP檔案中. 方便起見,單元測試檔案通常保存在 protected/tests/unit資料夾下.

  • 方法, 其中Abc通常是要被測試的類別方法.

  • 測試方法通常包含一系列斷言語句(e.g. assertTrue, asserEquals),作為驗證目標類行為的斷點。我們主要闡述如何為AR模型類別編寫單元測試. 我們的測試類別將會繼承自CDbTestCase,因為它提供了資料庫特定狀態支持,在上一章節中我們已經詳細討論了資料庫特定狀態.

  • 假設我們要測試blog案例中的Comment模型類,可以先建立一個命名為CommentTest的類,然後將它儲存為 
protected/tests/unit/CommentTest.php

:

class CommentTest extends CDbTestCase
{
    public $fixtures=array(
        'posts'=>'Post',
        'comments'=>'Comment',
    );

    ......
}
登入後複製

在這個類別中, 我們指定成員變數:

// return all rows in the 'Comment' fixture table
$comments = $this->comments;
// return the row whose alias is 'sample1' in the `Post` fixture table
$post = $this->posts['sample1'];
// return the AR instance representing the 'sample1' fixture data row
$post = $this->posts('sample1');
登入後複製
在這個類別中, 我們指定成員變數

:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">public function testApprove() { // insert a comment in pending status $comment=new Comment; $comment-&gt;setAttributes(array( &amp;#39;content&amp;#39;=&gt;&amp;#39;comment 1&amp;#39;, &amp;#39;status&amp;#39;=&gt;Comment::STATUS_PENDING, &amp;#39;createTime&amp;#39;=&gt;time(), &amp;#39;author&amp;#39;=&gt;&amp;#39;me&amp;#39;, &amp;#39;email&amp;#39;=&gt;[email protected]&amp;#39;, &amp;#39;postId&amp;#39;=&gt;$this-&gt;posts[&amp;#39;sample1&amp;#39;][&amp;#39;id&amp;#39;], ),false); $this-&gt;assertTrue($comment-&gt;save(false)); // verify the comment is in pending status $comment=Comment::model()-&gt;findByPk($comment-&gt;id); $this-&gt;assertTrue($comment instanceof Comment); $this-&gt;assertEquals(Comment::STATUS_PENDING,$comment-&gt;status); // call approve() and verify the comment is in approved status $comment-&gt;approve(); $this-&gt;assertEquals(Comment::STATUS_APPROVED,$comment-&gt;status); $comment=Comment::model()-&gt;findByPk($comment-&gt;id); $this-&gt;assertEquals(Comment::STATUS_APPROVED,$comment-&gt;status); }</pre><div class="contentsignin">登入後複製</div></div>在這個類別中, 我們指定成員變數fixtures 為一個包含這個測試要用到的特定狀態(fixtures)陣列。這個陣列表示從特定狀態名稱到模型類別的對應或特定狀態表名(e.g. 從 posts 到 Post). 注意當對應到特定狀態表名時,應該在資料表名稱前加上冒號前綴( e.g. <img src="https://img.php.cn/upload/article/000/000/194/d8454295409f69752ae1c667ca9e3c2e-0.gif" alt="Yii框架官方指南系列增補版39-測驗:單元測驗(Unit Testing)" class="wp-smiley"> ost

)來區別於映射到模型類別. 當使用模型類別名稱時,對應的表將會被看作特定狀態表。正如我們之前所描述的 ,當一個測試方法執行後特定狀態表每次將會被重置到某些已知的狀態。

特定狀態名稱允許我們在測試方法中以一種很方便的方式存取特定狀態資料. 下面的程式碼展示了典型的使用方法:
rrreee

Note: 如果一個特定狀態聲明使用它的資料表名(e.g. 'posts'=>'Yii框架官方指南系列增補版39-測驗:單元測驗(Unit Testing)ost'

), 那麼上述第三個使用方法將會無效,因為我們已經沒有任何與模型類別的關聯資訊了.

接下來,我們要編寫testApprove方法在Comment模型類別中測試approve方法

. 程式碼非常簡單明了: 首先我們插入一條待審核評論; 然後透過從資料庫取出資料來驗證這條帶審核評論; 最後我們呼叫approve方法並且透過審核。

rrreee

以上就是Yii框架官方指南系列增補版39-測試:單元測試(Unit Testing)的內容,更多相關內容請關注PHP中文網(www.php.cn)!


🎜
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!