Testing/Mocking 自定义控制台命令

Testing/Mocking Custom Console commands

我构建了自己的简单命令,我想测试一下。

基本上是这样的:

    <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class NeatCommand extends Command
{


    protected $signature = 'my:neat:command {input_file : path for a json-formatted file to analyze}';

    protected $description = 'analyze an array of values';

    public function __construct()
    {
        parent::__construct();

    }


    public function handle()
    {

        $inputFile=$this->argument('input_file');
        echo $inputFile;
    }



}

所以我写了这个简单的测试:

/**
 * @test
 * @group neat
 */
public function commandShowsHelloWorld()
{

    $defaultCommand=Artisan::call('my:neat:command');

}

我只是想在此阶段进行测试:缺少参数。但是当我现在 运行 它时,phpunit 将它作为错误:

There was 1 error:

1) App\Console\Commands\NeatCommandTest::commandShowsHelloWorld
RuntimeException: Not enough arguments.

所以我的问题是。我怎么能嘲笑整个事情……或者说 $this->shouldReturn('RuntimeException: Not enough arguments.'); 之类的话?

得到它的工作,但添加了预期异常的@annotation。

/**
 * @test
 * @group neat
 * @expectedException RuntimeException
 * @expectedExceptionMessage Not enough arguments.
 */
public function commandWithoutArgumentsCausesError()
{

    $defaultCommand=Artisan::call('my:neat:command');

}