Rana Faiz Ahmad
Rana Faiz Ahmad

Reputation: 1698

Is there a way to mock an artisan command and assert if it ran in Laravel 8?

I have a piece of code that executes an artisan command. I want to write a test to assert if it ran with the right options.

So, essentially I'm looking for some magic like:

Queue::fake(); and Queue::assertPushed() but for artisan commands.

Upvotes: 3

Views: 1691

Answers (1)

mrhn
mrhn

Reputation: 18976

I believe your best approach is to use facade mocks. This has a little less features than a faked class in Laravel, but in my simply case, it works great for artisan commands.

Controller code should use the facade.

public function yourRoute()
{
    Artisan::call('my:cmd');
}

Now you can utilize the facade mock, my simply example i wrote and tested it working.

public function testArtisanMock()
{
    Artisan::shouldReceive('call')
        ->once()
        ->with('my:cmd');

    $response = $this->get('/artisan');
    $response->assertOk();
}

Upvotes: 3

Related Questions