samedi 6 juin 2015

Laravel Unit Tests assertResonseOk returns false but page is loaded in browser

I'm trying unit testing in Laravel 5. I followed this answer and got it half working. I've made it so that user is always redirected to the login page if not authenticated. Once authenticated, the home page should be the base url. The test for redirecting to the login page passes, but the test for showing home page when the user is authenticated doesn't: Expected status code 200, got 500. If I login in the browser I get the home page just fine. Why is that?

This is my routes.php

Route::get('/', 'HomeController@index');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

And these are my tests:

/**
 * This test passes. You get redirected if no user is authenticated
 * @test
 * @return void
 */
public function it_redirects_to_login_if_not_authenticated()
{
    Auth::shouldReceive('check')->once()->andReturn(false);
    $response = $this->call('GET', '/');

    $this->assertFalse($response->isOk());
}

/**
 * This test doesn't pass. The user is authenticated but the test fails
 * 'Expected status code 200, got 500.'
 * @test
 */
public function it_returns_home_page_if_user_is_authenticated()
{
    Auth::shouldReceive('check')->once()->andReturn(true);
    $this->call('GET', '/');

    $this->assertResponseOk();
}

Aucun commentaire:

Enregistrer un commentaire