This test fails because it will never pass the Auth::attempt()
function call. I put a dd()
statement to prove that it won't make it.
If I remove the two Auth::shouldReceive()
the code will run the first dd()
statement.
If I keep even one Auth::shouldReceive()
the first dd()
statement will never get called.
If I add ->twice()
instead of ->once()
there are no errors thrown which is odd because it should complain that it has only been called once.
If I place a dd()
statement at the first line of the controller it does not run until I remove the Auth::shouldReceive()
functions.
I must be something silly that I'm not getting because I've looked over many tutorials.
Controller
public function postLogin() {
$email = Input::get('email');
$password = Input::get('password');
dd('Does not make it to this line with auth::shouldReceive() in the test');
if (Auth::attempt(array('email'=>$email, 'password'=>$password))) {
dd("Doesn't make it here either with auth::shouldReceive() mock.");
$user = Auth::user();
Session::put('user_timezone', $user->user_timezone);
return Redirect::to('user/dashboard')->with('message', 'You are now logged in!');
} else {
return Redirect::to('user/login')->with('message', 'Your username/password combination was incorrect')->withInput();
}
}
Test
public function testUserTimezoneSessionVariableIsSetAfterLogin()
{
$user = new User();
$user->user_timezone = 'America/New_York';
$user->email = '[email protected]';
$user->password = 'test';
$formData = [
'email' => '[email protected]',
'password' => '123',
];
\Auth::shouldReceive('attempt')->once()->with($formData)->andReturn(true);
\Auth::shouldReceive('user')->once()->andReturn($user);
$response = $this->call('POST', '/user/login', $formData);
$this->assertResponseStatus($response->getStatusCode());
$this->assertSessionHas('user_timezone');
}
The problem was that I had
parent::construct()
in the constructor of my UserController. Apparently this causes issues with mocks.I assumed this was necessary to have
parent::construct()
because I had a custom constructor in UserController.