Commands resolve dependencies type hinted in the handle method, not in the constructor. Try that.
Jul 7, 2020
7
Level 15
Testing artisan command fails to resolve mocks
Hi Guys,
I have a Artisan test which mocks a class, when I run the command, Laravel does not resolve the mock from the service container.
public function testHandle() {
$company = Company::whereName('durham-cc')->first();
factory(Interaction::class, 10)->create(['company_id' => $company->id]);
$mock = $this->mock(DataTransferClient::class)
->shouldReceive('uploadFile')
->withAnyArgs()
->once();
app()->offsetSet(DataTransferClient::class, $mock);
$this->artisan('interaction:durham-transfer')
->assertExitCode(0);
}
The command gets called, and in the constructor:
public function __construct(DataTransferClient $client, InteractionLogService $interactionLogService) {
$this->client = $client;
$this->interactionLogService = $interactionLogService;
}
I am resolving the DataTransferClient - but the real client gets resolved instead of the mock.
Any ideas?
Level 61
3 likes
Please or to participate in this conversation.