英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论