代码之家  ›  专栏  ›  技术社区  ›  rob006

codeception:canseeRecord()和seeRecord()的区别是什么?

  •  0
  • rob006  · 技术社区  · 8 年前

    我在yii2应用程序中使用了单元测试的codeception。yii2模块有一个文档 seeRecord() 方法。也有类似的 canSeeRecord() 没有任何在线文档,但在phpdoc中有附加信息:

    条件断言:测试失败时不会停止

    但这实际上意味着什么?在测试失败时,phpsorm只显示第一个错误,所以我看不出有什么区别。这两种电话有什么实际区别吗?

    $this->tester->seeRecord(MyModel::class, ['name' => 'rob006']);
    $this->tester->canSeeRecord(MyModel::class, ['name' => 'rob006']);
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   rob006    8 年前

    记录于 https://codeception.com/docs/03-AcceptanceTests#Conditional-Assertions

    通常,一旦任何断言失败,对这一点的进一步断言 将跳过测试。有时你不想要这个-也许你有一个 长期运行的测试,您希望它一直运行到最后。在这种情况下,你 可以使用条件断言。各 see 方法具有相应的 canSee 方法,以及 dontSee 有一个 cantSee 方法:

    $I->canSeeInCurrentUrl('/user/miles');
    $I->canSeeCheckboxIsChecked('#agree');
    $I->cantSeeInField('user[name]', 'Miles');
    

    每个失败的断言将显示在测试结果中,但它不会停止测试。

        2
  •  0
  •   rob006    8 年前

    结果证明phpdoc是正确的-断言失败后测试将继续。但在我的例子中,我被只报告第一个错误的phpsorm集成混淆了。

    我创建了简单测试:

    public function testTest() {
        // products table is empty - these tests always fail
        $this->tester->canSeeRecord(Product::class, ['id' => 1]);
        $this->tester->canSeeRecord(Product::class, ['id' => 2]);
        $this->tester->canSeeRecord(Product::class, ['id' => 3]);
    }
    

    phpsorm仅显示此测试中的第一个错误:

    enter image description here

    但当您从控制台运行此测试时,将报告所有三个错误:

    enter image description here