This is called inside a function so I cannot return any random value and then use the real Book model afterwards.
Jul 4, 2025
4
Level 1
Mock Static Function (find) of Model Book and return Same Model Book
I'm trying to mock the find static function in my eloquent model and return a mocked model back.
class Book extends Model {}
$bookMock = Mockery::mock(Book::class)->makePartial();
Mockery::mock('alias:' . Book::class)
->makePartial()
->shouldReceive('find')
->with(1)
->andReturn($bookMock);
However, this will fail with an error
Mockery\Exception\RuntimeException: Could not load mock ..., class already exists
This must be done in a single test, so running in different processes won't work.
How can I mock a static find function of eloquent model and return back a mocked model to use in the test?
Please or to participate in this conversation.