Laravel 测试 - 使用 Mockery 抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21976710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:05:02  来源:igfitidea点击:

Laravel Testing - Throwing exception with Mockery

phpunit-testinglaravelmockery

提问by bb89

I'm very new to Laravel and unit testing in general. I'm trying to write some tests for my AccountController and I've run into a road block.

我对 Laravel 和单元测试很陌生。我正在尝试为我的 AccountController 编写一些测试,但遇到了障碍。

I'm using Sentry to handle users and groups in the site. I'm trying to test that my controller is handling exceptions thrown by Sentry properly. So my controller method that handles the login POST looks like this:

我正在使用 Sentry 来处理站点中的用户和组。我正在尝试测试我的控制器是否正确处理了 Sentry 抛出的异常。因此,我处理登录 POST 的控制器方法如下所示:

public function postLogin(){

    $credentials = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    try{
        $user = $this->authRepo->authenticate($credentials, true);
        return Redirect::route('get_posts');
    }
    catch (Exception $e){
        $message = $this->getLoginErrorMessage($e);
        return View::make('login', array('errorMsg' => $message));
    }
}

authRepository is just a repository that uses Sentry to handle authentication. Now I want to test that when an email address is not specified a LoginRequiredException is thrown and the user sees the error message. Here is my test:

authRepository 只是一个使用 Sentry 处理身份验证的存储库。现在我想测试当未指定电子邮件地址时抛出 LoginRequiredException 并且用户看到错误消息。这是我的测试:

public function testPostLoginNoEmailSpecified(){

    $args = array(
        'email' => '[email protected]'
    );

    $this->authMock
        ->shouldReceive('authenticate')
        ->once()
        ->andThrow(new Cartalyst\Sentry\Users\LoginRequiredException);

    $this->action('POST', 'MyApp\Controllers\AccountController@postLogin', $args);

    $this->assertViewHas('errorMsg', 'Please enter your email address.');
}

However, the test is not passing. For some reason all it spits out is:

但是,测试并没有通过。出于某种原因,它吐出来的是:

There was 1 error:

1) AccountControllerTest::testPostLoginNoEmailSpecified
Cartalyst\Sentry\Users\LoginRequiredException: 

Am I using the andThrow() method incorrectly? If anyone can shed any light on what is going on it would be much appreciated.

我是否错误地使用了 andThrow() 方法?如果有人能阐明正在发生的事情,将不胜感激。

Thanks in advance!

提前致谢!

回答by bb89

So I actually just figured the problem out. Turns out it wasn't a problem with my unit tests at all but was actually just a namespacing issue. I forgot the backslash on the Exception class. So in my controller it should have been:

所以我实际上只是想出了问题。事实证明,我的单元测试根本不是问题,而实际上只是命名空间问题。我忘记了 Exception 类的反斜杠。所以在我的控制器中它应该是:

try{
    $user = $this->authRepo->authenticate($credentials, true);
    return Redirect::route('get_posts');
}
catch (\Exception $e){
    $message = $this->getLoginErrorMessage($e);
    return View::make('account.login', array('errorMsg' => $message));
}