I'm implementing unit tests into my Laravel 4 application but I'm stuck on mocking accessor attributes.
I have an Eloquent
model that has an Accessor
attribute in it. I'm trying to mock this model and return a value when this accessor attribute is called. But I can't find any solution for it to make it work.
My super simple user class.
class User extends Eloquent {
public function getFullNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
}
I tried the following:
$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; // --> " "
I just get an empty space back, indicating that the original function is still being called.