Laravel - 测试重定向后会发生什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27282519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Testing what happens after a redirect
提问by Enrique Moreno Tent
I have a controller that after submitting a email, performs a redirect to the home, like this:
我有一个控制器,在提交电子邮件后,执行重定向到主页,如下所示:
return Redirect::route('home')->with("message", "Ok!");
I am writing the tests for it, and I am not sure how to make phpunit to follow the redirect, to test the success message:
我正在为它编写测试,但我不确定如何让 phpunit 跟随重定向,以测试成功消息:
public function testMessageSucceeds() {
$crawler = $this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);
$this->assertResponseStatus(302);
$this->assertRedirectedToRoute('home');
$message = $crawler->filter('.success-message');
// Here it fails
$this->assertCount(1, $message);
}
If I substitute the code on the controller for this, and I remove the first 2 asserts, it works
如果我用控制器上的代码代替它,并删除前 2 个断言,它就可以工作
Session::flash('message', 'Ok!');
return $this->makeView('staticPages.home');
But I would like to use the Redirect::route
. Is there a way to make PHPUnit to follow the redirect?
但我想使用Redirect::route
. 有没有办法让 PHPUnit 跟随重定向?
回答by huntie
You can get PHPUnit to follow redirects with:
你可以让 PHPUnit 跟随重定向:
Laravel >= 5.5.19:
Laravel >= 5.5.19:
$this->followingRedirects();
Laravel < 5.4.12:
Laravel < 5.4.12:
$this->followRedirects();
Usage:
用法:
$response = $this->followingRedirects()
->post('/login', ['email' => '[email protected]'])
->assertStatus(200);
Note:This needs to be set explicitly for each request.
注意:这需要为每个请求明确设置。
For versions between these two:
对于这两者之间的版本:
See https://github.com/laravel/framework/issues/18016#issuecomment-322401713for a workaround.
有关解决方法,请参阅https://github.com/laravel/framework/issues/18016#issuecomment-322401713。
回答by jsawo
You can tell crawler to follow a redirect this way:
您可以通过这种方式告诉 crawler 遵循重定向:
$crawler = $this->client->followRedirect();
so in your case that would be something like:
所以在你的情况下,这将是这样的:
public function testMessageSucceeds() {
$this->client->request('POST', '/contact', ['email' => '[email protected]', 'message' => "lorem ipsum"]);
$this->assertResponseStatus(302);
$this->assertRedirectedToRoute('home');
$crawler = $this->client->followRedirect();
$message = $crawler->filter('.success-message');
$this->assertCount(1, $message);
}
回答by yesnik
In Laravel 6 to test redirect you can use assertRedirect:
在 Laravel 6 中测试重定向你可以使用assertRedirect:
/** @test */
public function store_creates_claim()
{
$response = $this->post(route('claims.store'), [
'first_name' => 'Joe',
]);
$response->assertRedirect(route('claims.index'));
}
回答by Ryan Firth
For Laravel 5.6, you can set
对于 Laravel 5.6,您可以设置
$protected followRedirects = true;
within your class file for your test case
在您的测试用例的类文件中