带有 RenderController 的 Symfony PHPUnit WebTestCase
Symfony PHPUnit WebTestCase with RenderController
我已经在我的应用程序中实现了这个测试
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,
]);
}
错误:
There was 1 failure:
1) App\Tests\Controller\HomeControllerTest::testHome Failed asserting
that 500 matches expected 200.
/var/www/html/app-web/tests/Controller/HomeControllerTest.php:15
为什么会这样?有没有办法在 Web 测试用例中处理 renderController?
我找到了。根据这个文档:https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests
您必须在 phpunit.xml.dist
的 php 部分添加环境。
<env name="DATABASE_URL" value="mysql://root:root@localhost/app" />
我已经在我的应用程序中实现了这个测试
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,
]);
}
错误:
There was 1 failure:
1) App\Tests\Controller\HomeControllerTest::testHome Failed asserting that 500 matches expected 200.
/var/www/html/app-web/tests/Controller/HomeControllerTest.php:15
为什么会这样?有没有办法在 Web 测试用例中处理 renderController?
我找到了。根据这个文档:https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests
您必须在 phpunit.xml.dist
的 php 部分添加环境。
<env name="DATABASE_URL" value="mysql://root:root@localhost/app" />