如何将 additionProvider 与存储库功能一起使用?
How to use additionProvider with repository functions?
我正在尝试为我的 Symfony 2.7 项目配置测试 类。我正在测试一个使用 doctrine 连接到数据库的控制器。
我终于成功地扩展了 KernelTestCase 以避免这个致命错误:调用非对象上的成员函数 "X"。但这就是问题所在:我试图通过使用 additionProvider 来订购我的代码并将 5 个测试函数简化为一个:
public function additionProvider()
{
$first=$this->service->getTranslation("","en");
return array
(
'original not created' =>array($first,"")
);
}
我想像这样使用它:
/**
* @dataProvider additionProvider
*/
public function testGetTranslation($expected, $actual)
{
$this->assertEquals($expected, $actual);
}
这是我的设置():
public function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager()
;
$this->service = new \DictionaryBundle\Controller\UtilController($this->em);
}
我试着像这样添加第一个测试,但错误又出现了,就像它无法访问存储库一样。那么,是否可以将 addittionProviders 与存储库功能一起使用?怎么样?
谢谢!
dataProvider 在设置方法之前执行。所以service
变量还没有初始化。
所以dataprovider方法只能returndata,需要将调用移到被测方法中的service
Here the paragraph of the doc:
Note All data providers are executed before both the call to the
setUpBeforeClass static method and the first call to the setUp method.
Because of that you can't access any variables you create there from
within a data provider. This is required in order for PHPUnit to be
able to compute the total number of tests.
我正在尝试为我的 Symfony 2.7 项目配置测试 类。我正在测试一个使用 doctrine 连接到数据库的控制器。
我终于成功地扩展了 KernelTestCase 以避免这个致命错误:调用非对象上的成员函数 "X"。但这就是问题所在:我试图通过使用 additionProvider 来订购我的代码并将 5 个测试函数简化为一个:
public function additionProvider()
{
$first=$this->service->getTranslation("","en");
return array
(
'original not created' =>array($first,"")
);
}
我想像这样使用它:
/**
* @dataProvider additionProvider
*/
public function testGetTranslation($expected, $actual)
{
$this->assertEquals($expected, $actual);
}
这是我的设置():
public function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager()
;
$this->service = new \DictionaryBundle\Controller\UtilController($this->em);
}
我试着像这样添加第一个测试,但错误又出现了,就像它无法访问存储库一样。那么,是否可以将 addittionProviders 与存储库功能一起使用?怎么样?
谢谢!
dataProvider 在设置方法之前执行。所以service
变量还没有初始化。
所以dataprovider方法只能returndata,需要将调用移到被测方法中的service
Here the paragraph of the doc:
Note All data providers are executed before both the call to the setUpBeforeClass static method and the first call to the setUp method. Because of that you can't access any variables you create there from within a data provider. This is required in order for PHPUnit to be able to compute the total number of tests.