4

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:

Transporter::observe(DefaultValuesObserver::class);

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.

Mazzy
  • 1,901
  • 2
  • 16
  • 36

1 Answers1

0

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

For reference http://docs.mockery.io/en/latest/reference/creating_test_doubles.html?highlight=alias%3A#overloading

moh96
  • 113
  • 1
  • 6