
我正在使用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只是一个使用Sentry来处理身份验证的存储库.现在我想测试一下,如果未指定电子邮件地址,则抛出LoginrequiredException并且用户会看到错误消息.这是我的测试:
public function testPostLoginNoEmailSpecifIEd(){ $args = array( 'email' => 'test@test.com' ); $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.');} 但是,测试没有通过.它出于某种原因吐出来的是:
There was 1 error:1) AccountControllerTest::testPostLoginNoEmailSpecifIEdCartalyst\Sentry\Users\LoginrequiredException:
我是否错误地使用了andThrow()方法?如果有人能够了解正在发生的事情,那将非常感激.
提前致谢!
所以我实际上只是想出了问题.事实证明,我的单元测试根本不是问题,但实际上只是一个命名空间问题.我忘记了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));} 总结 以上是内存溢出为你收集整理的php – Laravel Testing – 与Mockery一起抛出异常全部内容,希望文章能够帮你解决php – Laravel Testing – 与Mockery一起抛出异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)