In Laravel 5.2, I want to unit test my Eloquent User Repository.
class EloquentUserRepository implements UserRepositoryInterface
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function oneUser($id)
{
return $this->user->oneUser($id);
}
}
My test looks like below, with mocking the interface:
class EloquentUserRepositoryTest extends TestCase
{
public function setUp()
{
$this->user = factory(User::class, 1)->create(['name' => 'foo']);
}
/** @test */
public function it_fetch_an_user()
{
$mock = Mockery::mock('App\Repositories\Interfaces\UserRepositoryInterface')
->shouldReceive('oneUser')
->once()
->with($this->user->id)
->andReturn('foo');
App::instance(App\Repositories\EloquentUserRepository::class, $mock);
$userRepository = App::make(App\Repositories\EloquentUserRepository::class);
$this->assertEquals('foo', $userRepository->oneUser($this->user->id)->name);
}
public function tearDown()
{
Mockery::close();
}
}
I get this error:
ErrorException: call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Expectation' does not have a method 'oneUser'
I expect a simulated object that has the method oneUser, but it returns Mockery\Expectation. What do I wrong?