[英]What's the right way to add a custom Helper method for unit test suite in Codeception?
I am trying to add a custom helper method to unit test suite, but when running the test I get Fatal error: Uncaught ArgumentCountError: Too few arguments to function
error.我正在尝试向单元测试套件添加自定义帮助程序方法,但是在运行测试时出现
Fatal error: Uncaught ArgumentCountError: Too few arguments to function
错误的Fatal error: Uncaught ArgumentCountError: Too few arguments to function
。
This is what I have so far这是我到目前为止
When I run the test I get:当我运行测试时,我得到:
ArgumentCountError: Too few arguments to function ExampleTest::__construct(), 0
_support/Helper/Unit.php: _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 \Codeception\Module
{
public function get_hello()
{
return 'Hello';
}
}
Test method:测试方法:
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
- \Helper\Unit
Why doesn't testme1() accept any arguments?为什么 testme1() 不接受任何参数? What step am I missing?
我错过了什么步骤?
Unit tests methods don't get actor passed as a parameter.单元测试方法不会将 actor 作为参数传递。
You can call them on $this->tester
, like in this example你可以在
$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']);
}
Answer from @Naktibalda is correct for integration tests, not for unit tests. @Naktibalda 的回答对于集成测试是正确的,而不是单元测试。
Only way i find to get modul methods in unit tests is using getModule() method:我发现在单元测试中获取模块方法的唯一方法是使用 getModule() 方法:
public function testSomethink()
{
$this->getModule('Filesystem')->openFile('asd.js');
}
In this way you propably can load custom module too.通过这种方式,您也可以加载自定义模块。
If not, and you can reuse some code in unit test, make some parent class for all yout unit tests.如果没有,您可以在单元测试中重用一些代码,为所有单元测试创建一些父类。 Somethink like BaseUnitTest , which extends from Codeception\\Test\\Unit .
有些像BaseUnitTest ,它从Codeception\\Test\\Unit扩展。 And write your reusable code in this class.
并在此类中编写可重用代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.