在Codeception中为单元测试套件添加自定义帮助程序方法的正确方法是什么?



我正在尝试在单元测试套件中添加自定义助手方法,但是在运行测试时,我会出现Fatal error: Uncaught ArgumentCountError: Too few arguments to function错误。

这就是我到目前为止的

  1. 将方法添加到_support/helper/unit.php
  2. 运行构建命令
  3. suite.yml中的设置演员
  4. 通过演员调用该方法
  5. 运行测试

当我进行测试时,我会得到:

ArgumentCountError: Too few arguments to function ExampleTest::__construct(), 0

_support/helper/unit.php:


namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Unit extends CodeceptionModule
{
  public function get_hello()
  {
    return 'Hello';
  }
}

测试方法:

public function testMe1(UnitTester $I)
{
  $hello = $I->get_hello();
  $this->assertEquals(2, $hello);
}
# Codeception Test Suite Configuration
#
# Suite for unit (internal) tests.
class_name: UnitTester
modules:
  enabled:
    - Asserts
    - HelperUnit

为什么testme1((不接受任何参数?我缺少什么步骤?

单元测试方法不会使演员作为参数传递。

您可以在$this->tester上调用它们,例如在此示例中

function testSavingUser()
{
    $user = new User();
    $user->setName('Miles');
    $user->setSurname('Davis');
    $user->save();
    $this->assertEquals('Miles Davis', $user->getFullName());
    $this->tester->seeInDatabase('users', ['name' => 'Miles', 'surname' => 'Davis']);
}

@naktibalda的答案是对集成测试正确的,而不是单位测试。

我发现在单元测试中获得模量方法的唯一方法是使用getModule((方法:

public function testSomethink()
{
    $this->getModule('Filesystem')->openFile('asd.js');
}

这样您也可以加载自定义模块。

如果没有,您可以在单元测试中重复使用一些代码,为所有YouT单元测试进行一些父类。从 codeception test unit 延伸的baseunittest baseunittest somethink。并在此类中编写可重复使用的代码。

相关内容

最新更新