命令行测试

更新时间: 2019-07-31 20:15

期望输入/输出

Laravel 允许你使用 expectsQuestion 方法轻松地为控制台命令「模拟」用户输入。此外,你可以使用 assertExitCodeexpectsOutput 方法指定控制台命令退出码和期望输出文本。举个例子,想想下面这个控制台命令:

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.'.');
});

你可以使用下面的测试代码来测试命令,其中使用了 expectsQuestionexpectsOutputassertExitCode 方法:

/**
 * 测试控制台命令。
 *
 * @return  void
 */
public function test_console_command()
{
    $this->artisan('laracon')
         ->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);
}
Code Complete

Code Complete

Steve McConnell / Microsoft Press / 2004-6-19 / GBP 40.99

在线阅读本书 Widely considered one of the best practical guides to programming, Steve McConnells original CODE COMPLETE has been helping developers write better software for more than a decade. Now......一起来看看 《Code Complete》 这本书的介绍吧!

JSON 在线解析

JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码

Base64 编码/解码

Base64 编码/解码