-1

这是我要测试的代码

    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 = 'johndavedecano@gmail.com';

    $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

我的问题是为什么我会收到这个错误,因为我已经在我的测试中模拟了哨兵?

4

1 回答 1

0

这里不需要嘲笑。您只需要存根 ( Sentry) 和虚拟对象 ( User):

function it_returns_true_if_user_is_found(Sentry $sentry, User $user)
{
    $email = 'johndavedecano@gmail.com';

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

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

function it_returns_false_if_user_is_not_found(Sentry $sentry)
{
    $email = 'johndavedecano@gmail.com';

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

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

推荐阅读

于 2014-09-08T19:54:51.517 回答