Failed console command output expectations
Closed this issue · 0 comments
DarkGhostHunter commented
- Testbench Version: 6.19.0
- Laravel Version: 8.5.20
- PHP Version: 8.0.
- Database Driver & Version: SQLite 3
Description:
When testing commands created, output is never captured for assertion.
Steps To Reproduce:
- Create a Console command
<?php
namespace MyVendor\MyPackage;
use Illuminate\Console\Command;
class DummyCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sample:command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sample command';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->info('It works!');
return 0;
}
}
- Register the command into the application. For convenience, we will use the same test.
- Set output expectation.
<?php
namespace Tests\Console\Commands;
use Orchestra\Testbench\TestCase;
use MyVendor\MyPackage\DummyCommand;
class CommandTest extends TestCase
{
public function setUp(): void
{
\Illuminate\Console\Application::starting(function ($artisan) {
$artisan->resolveCommands([DummyCommand::class])
}
parent::setUp();
}
public function test_artisan_command(): void
{
$this->artisan('sample:command')
->expectsOutput('It works!');
->run();
}
}
- Test will fail.
Output "It works!" was not printed.