Laravel 4 Laravel unittest失败,找不到Auth::Test的匹配处理程序
我似乎遇到了一个问题,我的登录测试条件。我的登录处理程序检查电子邮件和密码是否有效,以及确认的用户是否有效。以下测试用例通过:Laravel 4 Laravel unittest失败,找不到Auth::Test的匹配处理程序,laravel-4,phpunit,mockery,Laravel 4,Phpunit,Mockery,我似乎遇到了一个问题,我的登录测试条件。我的登录处理程序检查电子邮件和密码是否有效,以及确认的用户是否有效。以下测试用例通过: public function test_redirect_to_login_if_login_fails() { // enable filters to test login Route::enableFilters(); // some fake credentials to use $credentials = [
public function test_redirect_to_login_if_login_fails()
{
// enable filters to test login
Route::enableFilters();
// some fake credentials to use
$credentials = [
'email'=>'user@domain.com',
'password'=>'badpassword',
'confirmed'=>1
];
// mock the Auth class
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(false);
// call the login post with the credentials
$this->call('POST','login',$credentials);
// assert that we are redirected back to the entry page
$this->assertRedirectedToRoute('entry');
}
public function test_redirect_if_login_success()
{
// enable filtere to test login
Route::enableFilters();
// some fake credentials to use
$credentials = [
'email'=>'user@domain.com',
'password'=>'Pa55w0rd',
'confirmed'=>1
];
// mock the Auth class
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(true);
// call the login post with the proper credentials
$response = $this->call('POST','login',$credentials);
// assert the response is good
$this->assertTrue($response->isRedirection());
}
然而,这一个给了我一个错误——尽管它与上面的第一个测试用例非常相似:
public function test_redirect_if_user_is_not_confirmed()
{
// enable filters to test login
Route::enableFilters();
// some fake credentials to use
$credentials = [
'email'=>'user@domain.com',
'password'=>'badpassword',
'confirmed'=>0
];
// mock the Auth class
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(false);
// call the login post with the credentials
$this->call('POST','login',$credentials);
// assert that we are redirected back to the entry page
$this->assertRedirectedToRoute('entry');
}
错误:
Mockry\Exception\NoMatchingExpecationException:没有匹配的处理程序
找到
mockry_0_Illumnate_Auth_AuthManager::trunt(数组('email'=>)user@domain.com“,”密码“=>”坏密码“,”确认“=>1,))。
该方法是意外的,或者其参数与预期值不匹配
此方法的参数列表
我的控制器方法:
public function store()
{
if (Auth::attempt(array("email"=>Input::get('email'),"password"=>Input::get('password'),'confirmed'=>1)))
{
return Redirect::intended('home');
}
return Redirect::route("entry")->with("danger",Lang::get('orientation.invalid'))->withInput();
}
您的测试未正确通过-因为您向测试提供了错误的数据 您有要接收的身份验证模拟:
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(false);
具体来说,它应该接收带有($credentials)的->
。但是您已经将$credentials
定义为拥有确认=>0
,而您的控制器总是要发送确认=>1
,因为这是硬编码的
相反-您应该仍然希望在测试中收到confirmed=>1
,但是模拟false
返回,因为找不到匹配的记录
这应该起作用:
// some fake credentials to use
$credentials = [
'email'=>'user@domain.com',
'password'=>'badpassword',
'confirmed'=>1
];
谢谢你的回复<代码>已确认不是输入字段。正常的流程是用户注册,收到一封电子邮件,然后在“我的用户”表中将
confirm
列从0设置为1。我的理解是(请参阅:使用条件验证用户)如果我需要设置其他需求,如确认状态,这就是如何完成的。如果这不是正确的方法,你知道正确的方法吗?