Hi I'm trying to create a test for a laravel controller not dependent on the view. I have the following method in my controller class:
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required',
'login'=>'required',
'password'=>'required'
]);
//TODO: Store to database
return redirect('usuario/nuevo');
}
And I found the following code to test whether the request had any errors:
public function testStore()
{
$response=$this->call('GET','usuario.store');
$this->assertSessionHasErrors();
}
As it is this test should pass since I'm sending a request without the required field filled out however PhpUnit returns the following message:
Session missing key: errors
Failed asserting that false is true.
The only way I can make it work is to try to "see" the error message on the response page by making the test like:
public function testStore()
{
$this->visit('/usuario/nuevo')
->press('Crear')
->see('Whoops');
}
This doesn't work for me for two reasons: 1)The test depends on the view to "press" the button and send the request(I wish to test this in a different test and keep this one strictly for the controller) and 2)The test depends on the 'Whoops' string to be present which is obviously bad practice.
I have found several answers claiming that the fist test code should work, but it simply doesn't.