I'm using Dependency injection to call custom services in Laravel and it works fine. But when i inject those dependencies into my Phpunit test case classes using interfaces, i receive the following error:
Target [App\Services\Interfaces\CarServiceInterface] is not instantiable.
although the interface has been bound to the target concrete class in the provider correctly.
i've used different styles like injecting through the __construct()
method, inject to test method or even calling the app()
method, but none of them works.
The test file:
private $carService;
public function setUp(): void
{
parent::setUp();
$this->carService = app(CarServiceInterface::class);
}
The provider:
$this->app->bind(
App\Services\Interfaces\CarServiceInterface::class,
App\Services\CarService::class
);
What is the correct way to do this?
CreatesApplication
trait in it.