如何在Laravel 5中测试工匠命令

问题描述 投票:31回答:3

我建立了一个Artisan命令来接收来自套接字的数据,我想为这个命令编写一个单元测试,但我不知道如何编写这样的测试。

有人知道怎么写吗?

laravel unit-testing laravel-5.1
3个回答
26
投票

现在要容易得多:

<?php

class YourCommandTest extends TestCase
{

    public function testExample()
    {
        $this->artisan('command', ['param' => 'value']);
    }

}

43
投票

测试示例

<?php

class YourCommandTest extends TestCase
{
    public function testExample()
    {
        Artisan::call('your_command', [
            'command_parameter_1' => 'value1',
            'command_parameter_2' => 'value2',
        ]);

        // If you need result of console output
        $resultAsText = Artisan::output();

        $this->assertTrue(true);
    }

}

1
投票

也许对某人有用

Laravel 5.7中的工匠指挥测试案例

public function test_console_command()
{
    $this->artisan('your_command')
         ->expectsQuestion('What is your name?', 'Ajay Makwana')
         ->expectsQuestion('Which language do you program in?', 'PHP')
         ->expectsOutput('Your name is Ajay Makwana and you program in PHP.')
         ->assertExitCode(0);
}

https://laravel-news.com/testing-artisan-commands-in-laravel-5-7

© www.soinside.com 2019 - 2024. All rights reserved.