Laravel 5 mock a Eloquent model

Solution:

The easiest way to mock an Eloquent model is by using partials:

$mock = m::mock('MyModelClass')->makePartial();

However, it won’t help you much as you’re using a static method (all()). PHP’s not-so-strict nature lets you call static methods in a non-static way ($user->all()), but you should avoid it. Instead you should do it the heavy-handed way:

$users = $this->user->newQuery()->get();

This can be mocked:

$mockUser->shouldReceive('newQuery->get')->andReturn([/*...*/]);

If you want to take this one step further, move the get() call into a separate repository class that is injected into the controller, which will make it easier to mock. You can find plenty of articles on the repository pattern online.