【发布时间】:2015-08-20 07:09:57
【问题描述】:
我正在我的 Laravel 4 应用程序中实现单元测试,但我被困在模拟访问器属性上。
我有一个 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; // --> " "
我只是得到一个空的空间,说明原来的函数还在被调用。
【问题讨论】: