assertRedirectedToRoute not working in Laravel 5.1

坚强是说给别人听的谎言 提交于 2019-12-13 04:47:54

问题


Controller code:

return redirect()->route('admin.patient.edit', $patientId);

Test code:

$this->visit(route('admin.patient.edit', $this->patient->id))
     ->press('Update');

$this->assertRedirectedToRoute('admin.patient.edit', [$this->patient->id]);

The error I get is this:

Failed asserting that Illuminate\Http\Response Object (...) is an instance of class 
"Illuminate\Http\RedirectResponse".

I've printed out the response from the inside the test and inside the controller and it is in fact a RedirectReponse Object. Any ideas?


回答1:


Try replacing

$this->assertRedirectedToRoute('admin.patient.edit', [$this->patient->id]);

with

$this->assertRedirectedToAction('MyController@myMethod');

You are already on the route admin.patient.edit

One route should be a GET request for the edit method, and the other should be a post request for the update() method. You are redirecting to the same exact route (i.e the GET route). admin.patient.update would be a better name for that route




回答2:


I came across same problem.

Please refer to this link. People has been complaining. Turn out that the way we use it was wrong. You can refer to the right way here

Whenever we want to use $this->assertRedirectedToRoute($route) or $this->assertRedirectedToAction($action) or similar, we cannot use $this->visit().

We must change it to $this->get()



来源:https://stackoverflow.com/questions/33946093/assertredirectedtoroute-not-working-in-laravel-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!