2014-12-29 2 views
0

조건이있는 로그인 테스트에 문제가있는 것 같습니다. 내 로그인 처리기는 이메일과 비밀번호가 유효 함을 확인하고 확인중인 사용자를 확인합니다. 다음 테스트 케이스 통과 :Auth :: 시도에서 일치하는 처리기가없는 Laravel unittest가 실패했습니다.

public function test_redirect_to_login_if_login_fails() 
{ 
    // enable filters to test login 
    Route::enableFilters(); 
    // some fake credentials to use 
    $credentials = [ 
     'email'=>'[email protected]', 
     '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'=>'[email protected]', 
      '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'=>'[email protected]', 
      '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'); 
    } 

오류 :

Mockery\Exception\NoMatchingExpecationException: No matching handler found for Mockery_0_Illumnate_Auth_AuthManager::attempt(array('email'=>'[email protected]','password'=>'badpassword','confirmed'=>1,)). Either the method was unexpected or its arguments matched no expected argument list for this method.

내 컨트롤러 방법 :

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

답변

0

테스트에 잘못된 데이터를 입력했기 때문에 테스트가 올바르게 실패합니다.

당신은 인증의 모의를받을 수 있습니다

Auth::shouldReceive('attempt') 
     ->once() 
     ->with($credentials) 
     ->andReturn(false); 

특히이 ->with($credentials)을받을 것을. 하지만 컨트롤러가 항상 confirmed => 1을 보내려고 할 때 $credentialsconfirmed => 0으로 정의했습니다. 그 이유는 이것이 하드 코딩되어 있기 때문입니다.

대신에 -하지만 일치하는 레코드가 발견되지 때문에 false 수익을 조롱 - 당신은 여전히 ​​테스트에 confirmed => 1를받을을 기대 해야합니다.

이 작동합니다 : 답장을

// some fake credentials to use 
     $credentials = [ 
      'email'=>'[email protected]', 
      'password'=>'badpassword', 
      'confirmed'=>1 
     ]; 
+0

감사합니다. 'confirmed '는 입력 필드가 아닙니다. 정상적인 흐름은 사용자가 등록하고 이메일을 받으면 내 사용자 테이블에서 'confirm' 열을 0에서 1로 설정합니다. [Laravel docs] (http://laravel.com/docs/4.2/security#authenticating-users) (조건을 가진 사용자 인증 참조)에서 얻은 나의 이해는 확인 된 상태와 같은 다른 요구 사항을 설정해야하는 경우 , 이것은 그것이 어떻게 행해지는지입니다. 이것이 올바른 방법이 아니라면 적절한 방법을 알고 있습니까? – gin93r