简体   繁体   English

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

[英]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这是我到目前为止

  1. Adding the method to _support/Helper/Unit.php将方法添加到 _support/Helper/Unit.php
  2. Running the build command运行构建命令
  3. Setup actor in suite.yml在suite.yml中设置actor
  4. Calling the method via the actor通过actor调用方法
  5. Running test运行测试

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.

相关问题 使用代码进行Laravel测试:单元测试模型的save()方法不会持久化 - Laravel testing with with codeception: unit test model's save() method doesn't persist 编码接收,配置套件测试路径 - Codeception, configure suite test path 在Laravel中对只读模型进行单元测试的正确方法是什么? - What's the right way to unit test a read-only model in Laravel? 在Zend Framework 1中注册自定义助手的正确方法是什么? - What is the right way to register a custom helper in Zend Framework 1? 代码接收单元测试 - Codeception unit test 在Codeception中,我应该将自定义方法添加到AcceptanceTester还是Helper / Acceptance-有区别吗? - In Codeception, should I add custom methods to AcceptanceTester or Helper/Acceptance - is there a difference? 使用 RedBeanPHP ORM 单元测试业务逻辑的正确方法是什么 - what is the right way to Unit test business logic with RedBeanPHP ORM Codeception 中的单元测试:如何将 Codeception 指向我的代码? - Unit test in Codeception: How to point Codeception to my code? 在Restler中使用自定义异常的正确方法是什么? - What's the right way to use custom exceptions in Restler? 在laravel 中的redirect() 助手上添加自定义方法 - add custom method on redirect() helper in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM