【问题标题】:How do I set method tearDown() in yii with phpunit?如何使用 phpunit 在 yii 中设置方法 tearDown()?
【发布时间】:2013-06-19 15:41:18
【问题描述】:

我为它写了一个类注册和测试。

$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]','1a1111@gmail.com');
    $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(),但结果是一样的。

【问题讨论】:

  • 请注意,根据APIdelete()创建并执行删除SQL语句,所以$exe->execute()是不必要的。
  • 没有$exe->execute()也不行。(
  • 请验证tearDown方法是否实际执行。如果它没有被执行,你不需要怀疑。如果已执行,请验证该删除命令是否确实有效。

标签: testing yii phpunit selenium-rc


【解决方案1】:

尝试另一种方式。例如,按属性(你知道它们)查找模型并将其删除。

User::model()->deleteAllByAttributes(array(
    'email' => '1a1111@gmail.com',
));

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 2019-12-26
    • 2018-07-04
    • 2023-03-11
    • 2015-08-10
    • 2012-12-06
    • 2014-01-11
    • 2019-06-14
    • 2013-12-28
    相关资源
    最近更新 更多