Symfony PHPUnit WebTestCase与RenderController

时间:2018-10-24 21:43:01

标签: php symfony phpunit symfony4

我已经在我的应用中实现了此测试

public function testHome()
{
    $client = static::createClient();
    $client->request('GET', '/');
    $this->assertEquals(200, $client->getResponse()->getStatusCode());
}

由于我的树枝中有一个渲染控制器,它失败了

{% extends 'base-pages.html.twig' %}

{% block main %}
    <main>
        {{ render(controller('App\\Controller\\AppController::futureEvents')) }}
        {{ include('includes/popular-places.html.twig') }}
        {{ include('includes/news-event.html.twig') }}
        {{ include('includes/call-section.html.twig') }}
    </main>
{% endblock %}

呈现的控制器:

public function futureEvents()
{
    $events = $this->getDoctrine()->getRepository(Event::class)->findAll();

    return $this->render('includes/events-home-list.html.twig', [
        'events' => $events,
    ]);
}

错误:

  

有1个失败:

     

1)App \ Tests \ Controller \ HomeControllerTest :: testHome断言失败   500个匹配预期的200个。

     

/var/www/html/app-web/tests/Controller/HomeControllerTest.php:15

为什么会这样?是否可以在Web测试用例中处理renderController?

1 个答案:

答案 0 :(得分:0)

我找到了。根据这份文件:https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests

您必须在phpunit.xml.dist的php部分中添加一个env。

<env name="DATABASE_URL" value="mysql://root:root@localhost/app" />