使用单独的数据提供者类与PHPUnit和属性

huangapple go评论62阅读模式
英文:

Using separate data provider class with PHPUnit and attributes

问题

我想要将测试和数据提供程序分开。使用 PHP 8 属性,当引用外部数据提供程序时,我无法使以下测试运行:

#[Test]
#[DataProviderExternal(RouterDataProvider::class, 'registerGetRouteData')]
public function itRegistersGetRoute(Route $route, array $expectedResult)
{
$this->router->get($route);
$this->assertEquals($expectedResult, $this->router->getRoutes());
}


我的数据提供程序类:

class RouterDataProvider
{
public static function registerGetRouteData(): array
{
return [
$route = new Route('/', ['IndexController', 'index']),
[
'GET' => [
'/' => $route,
],
'POST' => []
]
];
}
}


如何使这个测试使用所需的提供程序方法运行?
英文:

I would like to separate Tests and Data Providers. Using PHP 8 attributes, I cannot get the following test to run when referencing an external Data Provider:

#[Test]
#[DataProviderExternal(RouterDataProvider::class, 'registerGetRouteData')]
public function itRegistersGetRoute(Route $route, array $expectedResult)
{
    $this->router->get($route);
    $this->assertEquals($expectedResult, $this->router->getRoutes());
}

My data provider class:

class RouterDataProvider
{
    public static function registerGetRouteData(): array
    {
        return [
            $route = new Route('/', ['IndexController', 'index']),
            [
                'GET' => [
                    '/' => $route,
                ],
                'POST' => []
            ]
        ];
    }
}

How could I get this test to run with the desired provider method?

答案1

得分: 1

以下是翻译好的部分:

"By running PHPUnit with the following flags, I was able to see exactly what my issue was:"
"通过在PHPUnit中使用以下标志,我能够准确地看到我的问题是什么:"

"The data set was invalid. Changing the return to yield and updating the return type for the registerGetRouteData method from array to \Generator resolved this."
"数据集无效。将return更改为yield并将registerGetRouteData方法的返回类型从array更改为\Generator解决了此问题。"

"I was running phpunit with the --testdox flag, so I'm not sure if this is what stopped me seeing any errors initially and assume the test was being skipped."
"我正在使用--testdox标志运行phpunit,因此我不确定这是否阻止我最初看到任何错误,并假设测试被跳过。"

英文:

By running PHPUnit with the following flags, I was able to see exactly what my issue was:

./vendor/bin/phpunit --display-deprecations --display-warnings --diplay-errors --display-notices

The data set was invalid. Changing the return to yield and updating the return type for the registerGetRouteData method from array to \Generator resolved this.

I was running phpunit with the --testdox flag, so I'm not sure if this is what stopped me seeing any errors initially and assume the test was being skipped.

huangapple
  • 本文由 发表于 2023年2月9日 02:30:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定