13

我正在使用 Codeception 对我的 Laravel 4 PHP 应用程序进行单元、功能和验收测试。

我的单元测试如下所示:

use Codeception\Util\Stub;
class ExampleTest extends \Codeception\TestCase\Test 
{
 public function testExample()
 {
  $example = true;
  $this->assertSame($example, true);
 }
}

我的功能测试如下所示:

use \TestGuy;
class ExampleCest
{
 public function example(TestGuy $I)
 { 
  $I->amOnPage('/auth/login');
  $I->see('Sign in');
 }
}

但我也想在我的功能测试中使用 PHPUnit 断言方法。但是当我尝试时,我得到了这个错误:

调用未定义的方法 ExampleCest::assertSame()

如何在 Codeception 功能测试中使用 PHP 断言方法?

4

4 回答 4

30

由于Codeception 2.1(不是 2.0),您可以像其他断言一样使用它:

$I->assertSame($expected, $actual, $message);

但不要忘记Asserts在您的配置中启用该模块 - 例如:

class_name: UnitTester
modules:
    enabled: [ Asserts ]

请注意:升级到 2.1 时您可能需要更改配置 - 请参阅升级说明:http ://codeception.com/06-19-2015/codeception-2.1-rc.html

于 2015-08-05T17:03:34.653 回答
13

\PHPUnit_Framework_Assert::assertSame()

于 2014-01-30T02:13:46.420 回答
3

在 Codeception 4 中,只需添加断言模块:

modules:
    enabled:
        - \Codeception\Module\Asserts

到您的 suite.yml 配置文件并运行codeception build

于 2020-07-09T17:43:02.427 回答
2

另一种解决方法是在测试套件中使用辅助方法。

例如assertSame()方法

class ExpectedHelper extends \Codeception\Module
{
    protected $test;

    function _before(\Codeception\TestCase $test) {
        $this->test = $test;
    }

    function assertSame($expected, $actual, $message = '')
    {
        $this->test->assertSame($exception, $actual, $message);
    }
}

其中ExpectedHelper是测试套件助手名称(例如:UnitHelperFunctionalHelper),应该在_support文件夹下

你可以在你的测试中使用它$I->assertSame('12340','12340');

于 2015-04-20T08:01:37.477 回答