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);