2013-06-22 32 views
0

我寫了一個類註冊和測試它。如何使用phpunit在yii中設置方法tearDown()?

$this->open('/'); 

    if($this->isTextPresent('logout')) 
     $this->clickAndWait('link=logout'); 
    $this->clickAndWait('link=reg'); 
    $this->assertElementPresent('id=user-reg-form');     
    $this->type('name=User[login]','root111'); 
    $this->click("//input[@value='Submit']"); 
    $this->isTextPresent('cannot be blank.'); 
    $this->type('name=User[pswd]','ll111'); 
    $this->click("//input[@value='Submit']"); 
    $this->isTextPresent('cannot be blank.');  
    $this->type('name=User[email]','[email protected]'); 
    $this->click("//input[@value='Submit']"); 
    $this->waitForTextPresent('logout'); 
    $this->clickAndWait('link=logout'); 
    $this->waitForTextPresent('reg'); 
    $this->clickAndWait('link=reg'); 

並在WebTestCase.php中設置tearDown()以刪除新記錄。

//delete test user record 
    $exe = Yii::app()->db->createCommand(); 
    $exe->delete('{{user}}', 'id > :id', array(':id'=>5)); 
    $exe->execute(); 

根據需要執行測試,但不刪除記錄。爲什麼?我如何解決這個錯誤?我嘗試使用terDownAfterClass(),但結果是相同的。

+0

只是個頭,根據[API](http://www.yiiframework.com/doc/api/1.1/CDbCommand#delete-detail)'delete()'創建並執行刪除SQL語句,所以'$ exe-> execute()'是不必要的。 – topher

+0

沒有'$ exe->的execute()'不要太努力。( – volkov

+0

請確認'tearDown'方法真正執行與否。如果它不執行你不需要懷疑,如果它的執行,驗證如果該刪除命令實際工作。 – hakre

回答

0

嘗試另一種方式。例如按屬性查找模型(你知道他們)並刪除它。

User::model()->deleteAllByAttributes(array(
    'email' => '[email protected]', 
)); 
相關問題