2014-08-31 40 views
-1

懲戒時調用一個成員函數這是我試圖測試致命錯誤:在PHPSpec

public function forgot($email) 
{ 
    try 
    { 
     // Find the user using the user email address 
     $user =$this->sentry->findUserByLogin($email); 

     $resetCode = $user->getResetPasswordCode(); 

     return true; 
    } 
    catch (UserNotFoundException $e) 
    { 
     $this->errors[] = 'User was not found.'; 

     return false; 
    } 
} 

下面的代碼是我的測試代碼

function it_should_forgot(Sentry $sentry) 
{ 
    $email = '[email protected]'; 

    $sentry->findUserByLogin($email)->shouldBeCalled(); 

    $this->forgot($email); 
} 

以下是錯誤

PHP Fatal error: Call to a member function getResetPasswordCode() on a non-object in /var/www/laravel/app/Services/SentryService.php on line 103 

我的問題是爲什麼我得到這個錯誤,因爲我已經在我的測試中嘲笑哨兵?

+0

那麼,做你嘲笑'findUserByLogin'返回什麼? – zerkms 2014-08-31 12:58:20

+0

請賜教我應該怎麼做。謝謝 – 2014-08-31 13:02:47

+0

那麼,看看你的測試方法,並告訴它根據它應該返回'findUserByLogin'方法。 – zerkms 2014-08-31 13:05:04

回答

0

這裏沒有嘲諷。你只需要存根(Sentry)和傻瓜(User):

function it_returns_true_if_user_is_found(Sentry $sentry, User $user) 
{ 
    $email = '[email protected]'; 

    $sentry->findUserByLogin($email)->willReturn($user); 

    $this->forgot($email)->shouldReturn(true); 
} 

function it_returns_false_if_user_is_not_found(Sentry $sentry) 
{ 
    $email = '[email protected]'; 

    $sentry->willThrow(new UserNotFoundException())->duringFindUserByLogin($email); 

    $this->forgot($email)->shouldReturn(false); 
} 

推薦閱讀