Skip to content
Advertisement

Mocking classes that created with app()->make

I have this code in my __construrct:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
    $this->dbUserService = app()->make('DBUserService');
}

Now, when i’m unit testing i know that i can mock Guard and pass it’s mock to $auth, but how can i mock dbUserService ? it’s instantiated trough the IoC container.

Advertisement

Answer

You can use the instance() method of the IoC container to mock any class instanciated with make():

$mock = Mockery::mock(); // doesn't really matter from where you get the mock
// ...
$this->app->instance('DBUserService', $mock);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement