简体   繁体   English

Symfony 2功能测试

[英]Symfony 2 Functional test

I encounter an issue which doesn't make much logic to me in one of my functional tests. 我在一个功能测试中遇到了一个对我来说并没有太多逻辑的问题。

The test is quite simple, it just requests for the homepage, and searches for an element with a certain class in it. 该测试非常简单,它只请求主页,然后搜索其中具有特定类的元素。

The homepage is for a blog that displays a list of articles. 主页用于显示文章列表的博客。 And each article can have multiple pages (One-To-Many), each with it's own featured image. 每个文章可以有多个页面(一对多),每个页面都有自己的特色图片。

On homepage (in twig) I do $article->getPages()->first()->getImageUrl(). 在主页上(在树枝上),我执行$ article-> getPages()-> first()-> getImageUrl()。

If I use the same fixtures and I access the page from the browser everything is displayed as it should, but when I run the test it throws an exception "Could not access getImageUrl on unknown object", which makes me think it's not pulling the article pages from the DB. 如果使用相同的灯具,并且从浏览器访问页面,则所有内容均会显示,但是在运行测试时,它将引发异常“无法在未知对象上访问getImageUrl”,这使我认为它没有拉出文章。数据库中的页面。

To make this even stranger, if I query myself the db for the articles directly in the test and iterate over the articles they all work ($article->getPages()->first()->getImageUrl()). 为了使这个问题变得更加陌生,如果我直接在测试中查询数据库中的文章并遍历这些文章,它们都可以工作($ article-> getPages()-> first()-> getImageUrl())。 Only when using the self::$client->request() call it's not working. 只有在使用self :: $ client-> request()调用时,它才起作用。

I doble checked the fixtures, I even added an explicit innerJoin() on the query builder, and manually checked that the database has the right data. 我检查了固定装置,甚至在查询生成器上添加了显式的innerJoin(),并手动检查数据库是否具有正确的数据。

Moreover, I have other functional tests that load their own fixtures and they all work, except this one. 而且,除了此功能外,我还有其他功能测试可加载自己的灯具,并且它们都可以正常工作。

This is the code of my test below. 这是我下面的测试代码。 It fails both when run solely and when executed with the entire suite of tests. 无论是单独运行还是与整套测试一起执行,它都会失败。

public function testSocialBoxPositionWithFeatured()
{
    self::runCommand('h4cc_alice_fixtures:load:files --drop src/SNN/AdminBundle/Fixtures/Nelmio.yml');

    $crawler = self::$client->request('GET', '/'); //go to homepage

    $classes = $crawler
            ->filter('.postsContainer > .col-md-4')
            ->eq(5)
            ->children()
            ->first()
            ->attr("class");

    $this->assertContains("socialBox", $classes);
}

Thanks in advance, 提前致谢,

我发现清除实体管理器可以使我的测试正常工作...所以我只添加了self :: $ client-> getContainer()-> get('doctrine')-> getManager()-> clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM