【问题标题】:ArgumentCountError : Too few arguments to function phpunitArgumentCountError : 函数 phpunit 的参数太少
【发布时间】:2021-03-17 23:27:17
【问题描述】:

我正在尝试使用phpUnit 创建一些编程测试。 我需要使用数据提供程序,但每次尝试时都会引发错误。 我什至在使用phpUnit 文档中给出的示例。

 /**
 * @dataProvider additionWithNonNegativeNumbersProvider
 */
public function testAdd($a, $b, $expected)
{
    $this->assertSame($expected, $a + $b);
}

public function additionWithNonNegativeNumbersProvider()
{
    return [
        [0, 1, 1],
        [1, 0, 1],
        [1, 1, 3]
    ];
}

我希望输出是:

There was 1 failure:
1) DataTest::testAdd with data set #3 (1, 1, 3)
Failed asserting that 2 is identical to 3.

但它是:

ArgumentCountError : Too few arguments to function controllerTests::testAdd(), 0 passed in phar://C:/xampp/htdocs/2019-1-qa-grupo1/myss/Tests/phpunit-8.1.2.phar/phpunit/Framework/TestCase.php on line 1172 and exactly 3 expected
 C:\xampp\htdocs\2019-1-qa-grupo1\myss\Tests\controllerTests.php:55

【问题讨论】:

  • 我无法复制它。您是否在测试类中定义了构造函数?如果您不调用父构造函数,这可能会破坏事情:stackoverflow.com/questions/10175414/…
  • 我已经定义了我的构造函数。但它调用父构造函数。我刚刚意识到我的 IDE 显示消息“类父级存在多个定义”
  • 就像我链接到的那个问题的第二个答案中显示的那样?致电parent::__construct() 将不起作用。 (只是确保)
  • 谢谢,我没有看到第二个答案。

标签: php phpunit


【解决方案1】:

您需要将可选参数传递给父构造函数。

如果你的测试课中有这个:

<?php
public function __construct() {
    // Some constructor code.
}

改成:

<?php
public function __construct($name = null, array $data = [], $dataName = '') {
    parent::__construct($name, $data, $dataName);
    // Some constructor code.
}

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 2019-06-01
    • 1970-01-01
    • 2022-08-17
    • 2018-10-11
    • 2018-02-08
    • 2020-07-30
    • 2018-06-26
    • 2021-03-17
    相关资源
    最近更新 更多