终端测试
简介
除了简化 HTTP 测试外,Laravel 还提供了简单的 API 用于测试询问用户输入的终端应用。
期望输入/输出
Laravel 允许您使用 expectsQuestion
方法轻松「模拟」用户输入的终端命令。此外,还可以使用 assertExitCode
和 expectsOutput
方法指定退出码以及期望终端命令输出的文本。例如,有如下终端命令:
Artisan::command('question', function () {
$name = $this->ask('What is your name?');
$language = $this->choice('Which language do you program in?', [
'PHP',
'Ruby',
'Python',
]);
$this->line('Your name is '.$name.' and you program in '.$language.'.');
});
可以使用 expectsQuestion
,expectsOutput
和 assertExitCode
方法测试以上命令:
/**
* 测试终端命令
*
* @return void
*/
public function test_console_command()
{
$this->artisan('question')
->expectsQuestion('What is your name?', 'Taylor Otwell')
->expectsQuestion('Which language do you program in?', 'PHP')
->expectsOutput('Your name is Taylor Otwell and you program in PHP.')
->assertExitCode(0);
}