我正在尝试为我的 Symfony 2.7 项目配置测试类。我正在测试一个使用学说连接数据库的 Controller 。
我最终设法扩展 KernelTestCase 以避免这个 fatal error :调用非对象上的成员函数“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);
}
我尝试像这样添加第一个测试,但错误再次出现,就像它无法访问存储库一样。那么,是否可以将 additionProviders 与存储库功能一起使用?怎么办?
谢谢!
最佳答案
dataProvider 在设置方法之前执行。因此 service
变量尚未初始化。
因此dataprovider方法只能返回数据,您需要将调用移至测试方法中的服务。
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.
关于php - 如何将additionProvider与存储库功能一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32700752/