Symfony 4 - 功能测试 - 虽然followRedirects是真的,但客户端不遵循重定向

问题描述 投票:2回答:1

我一直在尝试在我正在处理的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响应。

在此先感谢有关如何解决此问题的任何建议或答案:)

php symfony phpunit
1个回答
0
投票

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

因此,在你完成你的请求之后设定......好吧......没用。

所以......改变

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

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

它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.