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 my
AppServiceProvider.php I register my observer:
So when I comment this line out, the mocking works fine.
But when this line is present, I get a
Mockery\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) ->shouldReceive('getAttribute') ->passthru() ->shouldReceive('setAttribute') ->passthru();
But this is not the best solution if I have to do this everywhere.