I'm doing unit tests in my Laravel 4 application, but I'm stuck in mocking access attributes.
I have a model Eloquentin which there is an attribute Accessor. I am trying to mock this model and return a value when this accessor attribute is called. But I cannot find a solution for this to work.
My super simple custom 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');
$user_mock->shouldReceive('getAttribute')->with('full_name')->andReturn('John Snow');
$user_mock->shouldReceive('getFullNameAttribute')->andReturn('John Snow');
echo $user_mock->full_name;
I just get the empty space back, indicating that the function is still being called.
source
share