2014-08-31 62 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); 
} 

推荐阅读