我有一个控制器,在提交电子邮件后,执行重定向到家庭,如下所示:
return Redirect::route('home')->with("message","Ok!");
我正在为它编写测试,我不知道如何让PHPunit按照重定向来测试成功消息:
public function testMessageSucceeds() { $crawler = $this->client->request('POST','/contact',['email' => 'test@test.com','message' => "lorem ipsum"]); $this->assertResponseStatus(302); $this->assertRedirectedToRoute('home'); $message = $crawler->filter('.success-message'); // Here it fails $this->assertCount(1,$message); }
Session::flash('message','Ok!'); return $this->makeView('staticPages.home');
你可以让PHPUnit跟随重定向:
Laravel> = 5.5.19:
$this->followingRedirects();
Laravel< 5.4.12:
$this->followRedirects();
用法:
$response = $this->followingRedirects() ->post('/login',['email' => 'john@example.com']) ->assertStatus(200);
注意:需要为每个请求明确设置.
对于这两者之间的版本:
有关解决方法,请参阅https://github.com/laravel/framework/issues/18016#issuecomment-322401713.