I am working with some unit testing and i have difficult with one kind of assertions
$this->assertRedirectedTo
Here is my very basic example controller code
// example.com/form
public function postForm()
{
$data = "my data";
$this->mylogic($data);
}
private function mylogic($data){
// operations with $data ... after some logics
return redirect()
->route('Gracias')
->send();
}
And this is my very basic example of my unit testing
public function REDRIRECT_TEST()
{
$this->call('POST','form'); // it works
$this->assertRedirectedTo('gracias'); // it fails
}
and I get this error
Failed asserting that Illuminate\Http\Response Object (...) is an instance of class "Illuminate\Http\RedirectResponse".
This error happen when i use redirect inside another method
private function mylogic($data){
return redirect()
->route('Gracias')
->send();
}
Any idea how to deal with assertRedirectedTo
where the redirect()
is inside another method?
Edited . Well, i re-thinked everythink and divided the logic from controller, now the logic is in another class and only return the correct route to redirect to the controller and my unit test pass.
In laravel 5.7+ the methods are a little changed for the unit test.
Session::start();
$response = $this->call('POST', 'form', [
'_token' => \Session::token(),
'var' => 'value',
]);
$response->assertStatus(302);
$response->assertRedirect('gracias');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.