php - Mocking Laravel Eloquent models - Mocking model that is used in AppServiceProvider


I am trying to mock an eloquent model using the alias directive as follows:

$transporter = \Mockery::mock('alias:' . Transporter::class)

I have added the following annotations to the class

* @runTestsInSeparateProcesses
* @preserveGlobalState disabled

The problem is that in myAppServiceProvider.php I register my observer:


So when I comment this line out, the mocking works fine. But when this line is present, I get aMockery\Exception\RuntimeException: Could not load mock App\Models\Laravel\Transporter, class already exists * This is a filtered run of phpunit where only one class is ran.

I suppose that when calling observe, it is autoloading the transport class already.

So even when running everything in a seperate process, the class was already registered in the AppServiceProvider.

My current fix is to not use the alias but use the passthru to overcome the Eloquent issues:

$transporter = \Mockery::mock(Transporter::class)

But this is not the best solution if I have to do this everywhere.




Try using$mock = \Mockery::mock('overload:MyClass');

For reference

People are also looking for solutions to the problem: Displaying PHP Errors from admin-ajax.php in Wordpress 4.9+


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.