php - 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 响应。
提前感谢您提供有关如何解决此问题的任何建议或答案:)
解决方案
followRedirects
是一个设置。它用于之后完成的每个请求。
所以在你完成你的请求之后设置是......好吧......没用。
所以……改变
$client->request('GET', '/fr');
$client->followRedirects(true);
至
$client->followRedirects(true);
$client->request('GET', '/fr');
它应该可以工作。
推荐阅读
- java - Android Room:迁移未正确处理崩溃 - 预期 INTEGER 找到 DATE
- visual-studio-code - 使用 quickOpen 时可选择触发 keepEditor 的 VSCode 键盘快捷键?
- amazon-s3 - S3A 客户端和本地 S3 模拟
- javascript - LokiJS 是否支持 ISO-8601 日期格式?
- python - 将基本网址加入到scrapy中的相对网址
- eclipse - 在 Eclipse 中更改 Cmake 生成器
- html - 在相对部分使用多个斜杠解析 URI
- powershell - 如何在没有科学记数法的情况下将长整数转换为字符串
- javascript - 如何更改密码重置电子邮件文本 google firebase
- r - 提取其值包含特定字符串 (R) 的变量名称