Laravel Unit Tests assertResonseOk返回false但页面在浏览器中加载

时间:2015-06-06 16:19:37

标签: unit-testing phpunit laravel-5

我正在Laravel 5中进行单元测试。我跟着this answer并完成了一半的工作。我做到了这样,如果没有经过身份验证,用户总是被重定向到登录页面。经过身份验证后,主页应该是基本URL。重定向到登录页面的测试通过,但是在用户通过身份验证时显示主页的测试不会:

Expected status code 200, got 500.

如果我在浏览器中登录,我的主页就好了。那是为什么?

routes.php文件

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

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

我的测试

/**
 * 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();
}

有人可以帮忙吗?

0 个答案:

没有答案