How to mock the static methods of a bright model?

I have a line like this in my code:

ModelName::create($data);

where ModelName is just a model Eloquent. Is there a way to mock this call inside unit test? I tried:

$client_mock = \Mockery::mock('Eloquent','App\Models\ModelName');
$client_mock->shouldReceive('create')
            ->with($data)->andReturns($returnValue);

but that will not work.

+4
source share
1 answer

You should do something like this:

$client_mock = \Mockery::mock('overload:App\Models\ModelName');
$client_mock->shouldReceive('create')->with($data)->andReturn($returnValue);

We use overload:it because you do not want to pass the layout to any class, but you want to use it in the case of hard coding in some classes.

In addition to your test class (before class) you should add:

/**
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */

, ( , , , ).

Mocking , .

UPDATE

, . ( orverload) :

App::instance('\App\Models\ModelName',$client_mock); 
+6

All Articles