首页 > 解决方案 > Symfony 4 - 功能测试 - 尽管 followRedirects 为真,但客户端不遵循重定向

问题描述

我一直在尝试在我正在处理的 Symfony 4 应用程序中开始实施一些功能测试,但我未能被重定向并最终从我的索引页面获得 200 响应代码,即使我在访问它时最终得到 200 响应通过本地 symfony 服务器。

这是有问题的测试

class MainControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $client->request('GET', '/fr');
        $client->followRedirects(true);
        var_dump($client->isFollowingRedirects());
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}

这是phpunit的输出:

#!/usr/bin/env php
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite
Fbool(true)
...                                                                4 / 4 (100%)

Time: 888 ms, Memory: 22.00MB

There was 1 failure:

1) App\Tests\Controller\MainControllerTest::testIndex
Failed asserting that 301 matches expected 200.

FollowRedirects 确实设置为 true,但当我应该得到 200 时,我仍然得到 301 响应。

提前感谢您提供有关如何解决此问题的任何建议或答案:)

标签: phpsymfonyphpunit

解决方案


followRedirects是一个设置。它用于之后完成的每个请求。

所以在你完成你的请求之后设置是......好吧......没用。

所以……改变

    $client->request('GET', '/fr');
    $client->followRedirects(true);

    $client->followRedirects(true);
    $client->request('GET', '/fr');

它应该可以工作。


推荐阅读