问题
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