我正在执行到我的Laravel 4应用程序的单元测试,但我坚持嘲笑访问属性。如何模拟Laravel的雄辩访问属性
我有一个Eloquent
模型,其中有一个Accessor
属性。我试图嘲笑这个模型,并在调用这个访问器属性时返回一个值。但是我找不到任何解决方案来使它工作。
我的超级简单的用户类。
class User extends Eloquent {
public function getFullNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
}
我试过如下:
$user_mock = m::mock('MyApp\Models\User');
$user_mock->shouldReceive('__get')->with('full_name')->andReturn('John Snow'); // doesn't work
$user_mock->shouldReceive('getAttribute')->with('full_name')->andReturn('John Snow'); // doesn't work
$user_mock->shouldReceive('getFullNameAttribute')->andReturn('John Snow'); // doesn't work
echo $user_mock->full_name; // --> " "
我刚刚得到一个空的空间回来,这表明原有的功能仍然被调用。
这是为什么downvoted?这不行?不要低估没有明确的理由,如果你这样做,请添加评论解释。 – VinGarcia