Laravel 测试artisan控制台命令时,对integer调用成员函数expectsOutput()

Laravel 测试artisan控制台命令时,对integer调用成员函数expectsOutput(),laravel,laravel-artisan,Laravel,Laravel Artisan,我有一个非常简单的例子来说明这个问题: <?php namespace App\Console\Commands; use Illuminate\Console\Command; class VendorCounts extends Command { /** * The name and signature of the console command. * * @var string */ protected $signat

我有一个非常简单的例子来说明这个问题:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class VendorCounts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'vendor:counts
                            {year : The year of vendor counts}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Runs vendor counts';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('Starting Vendor Counts');
    }
}
我知道该命令肯定会运行,因为如果我在其中放入dump语句,它将显示调试输出


我使用的是Laravel6.3。是否有其他方法来测试此问题?

是否可以将此添加到您的
vendorcountst
类中:

public $mockConsoleOutput = true;

这是由一个特征设置的,但只是确保某些东西没有改变值。当
$mockConsoleOutput
false
时,它将直接运行artisan命令。当它为
true
时,它将把它包装在一个
PendingCommand
对象中,该对象具有您试图调用的那些方法。

我使用的问题是
TestCase
正在使用
Laravel\browserkitting\TestCase
作为
BaseTestCase
。我最终只为控制台命令创建了另一个基础

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class ConsoleTestCase extends BaseTestCase
{
    use CreatesApplication;
}

我遇到了一个问题,在Artisan类上使用
expectedOutput()
会一直失败,这是因为我在一个方法中使用了
exit()
和/或
die()
,而这个方法在phpunit测试方法中确实不起作用

因此,如果您想在某个时候停止处理“脚本”,只要使用一个空的
返回
,而不是
exit()
die()
,如果您想在Laravel中使用内置的
->artisan()
测试

工作示例:
Hmm刚刚试过,但没用。同样的问题。根据库代码,您必须弄清楚为什么该变量最终为
false
。。。你改变了你正在使用的基本测试类吗?啊,就是这样。我正在使用
BrowserKitTesting
。一旦我切换到默认状态,问题就消失了。谢谢。这是升级到Laravel 6吗?
public $mockConsoleOutput = true;
<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class ConsoleTestCase extends BaseTestCase
{
    use CreatesApplication;
}