【发布时间】:2015-04-10 14:33:46
【问题描述】:
我正在尝试为几个控制器操作编写 phpunit 测试。除了嘲笑密码门面之外,一切似乎都很好。
其中一个操作(“忘记密码”)如下所示:
if (Auth::check())
//return a redirect since the user is already logged in
switch ($response = Password::remind(Input::only('email')))
{
case Password::INVALID_USER:
//return a redirect with error messages
case Password::REMINDER_SENT:
//return a redirect with success message flushed
}
Auth 和 Password 都是门面,默认是 Laravel 自带的。
测试Auth::check() 部分工作正常:
Auth::shouldReceive('check')->
once()->
andReturn(false); //or true depending on my test case
但是当我尝试对Password::remind() 调用相同的尝试时,它不起作用:
Password::shouldReceive('remind')->
once()->
andReturn(Password::INVALID_USER);
我收到“SQLSTATE Access denied”异常,这意味着应用程序正在尝试访问数据库。
我还尝试将with(array()) 添加到我的测试中,甚至绑定Mockery::mock('\Illuminate\Auth\Reminders\PasswordBroker') - 这些都没有帮助。
如何测试这个控制器动作?为什么 Password 外观与 Auth 不同?我还应该尝试什么?
谢谢
【问题讨论】:
标签: php testing laravel phpunit mockery