How to mock a flamboyant model with Mockery?
I am trying to do this:
$this->dsMock = Mockery::mock('Eloquent', 'API\V1\DataSet');
$this->app->instance('API\V1\DataSet', $this->dsMock);
$this->dsMock->shouldReceive('isLocalData')->once()->andReturn(true);
Then, inside the class under test:
$test = DataSet::isLocalData($dataSetId);
However, the DataSet class is not mocked. He is still trying to access the database. Why?
The likely problem is that Laravel is unfortunate about using Facade (which are also factories). If it was DataSet
already created using FaΓ§ade, it will continue to return the same class, you won't get the mocked version.
I can't remember if you can instantiate your class without using Facade in Laravel. You have to remember that when you call DataSet
statically in an application, you are not actually referring to API\V1\DataSet
, but something else that controls it.