【发布时间】:2014-10-24 20:56:50
【问题描述】:
这是我要测试的代码
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
我的问题是为什么我会收到这个错误,因为我已经在我的测试中模拟了哨兵?
【问题讨论】:
-
那么你在哪里模拟
findUserByLogin返回的内容? -
请告诉我应该怎么做。谢谢
-
好吧,看看你的测试方法,告诉你
findUserByLogin方法应该返回什么。 -
$sentry->findUserByLogin('johndavedecano@test.com')->willReturn($user);请让我知道这是否正确
-
如果你先试试呢? Stackoverflow 并非旨在成为语法检查器
标签: php unit-testing laravel-4 phpspec