php - 如何将additionProvider与存储库功能一起使用?

标签 php symfony doctrine-orm phpunit

我正在尝试为我的 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/

相关文章:

php - 限制和偏移多对多关系

javascript - 使用 D3.js 从 MySQL 数据库绘制条形图

php - 如何在开发模式下启用缓存?

php - Doctrine 选择空值

php - 如何根据相关OneToMany关联中的条件选择一组实体?

php - 如何获取使用 FPDF 生成的文档的宽度和高度

javascript - 在表单之外使用 PHP 的第二个提交按钮

symfony - 使用 "getParent()"覆盖模板似乎不起作用

php - 使用 Symfony2 + PHPUnit 的问题

symfony - Doctrine PostLoad 生命周期回调未在 fetchJoin 中执行