【问题标题】:artisan command testing Output "+----+----------+------------+----------------------+" was not printed工匠命令测试输出“+----+----------+------------+--------------- --------+" 未打印
【发布时间】:2021-01-29 03:26:24
【问题描述】:

我正在尝试测试一个工匠命令,该命令将给定客户 ID 的数据库表作为输出。 在 Laravel 8 上。

php artisan get:report "1"
+----+----------+------------+---------------------+
| id | customer | date       | value               |
+----+----------+------------+---------------------+
| 1  | 1        | 01/04/2015 | EUR 55.201818920353 |
| 5  | 1        | 02/04/2015 | EUR 12.188561617614 |
| 6  | 1        | 02/04/2015 | EUR 1.00            |
| 7  | 1        | 03/04/2015 | EUR 14.050917779273 |
+----+----------+------------+---------------------+

所以我写了这个单元测试:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;


class GetReportTest extends TestCase
{
    
    protected $tableHeader=['id', 'customer', 'date','value'];
    protected $rows=[[1,'1','01/04/2015' , 'EUR 55.201818920353'],
                        [5,'1' ,'02/04/2015',' EUR 12.188561617614'],
                        [6,'1','02/04/2015','EUR 1.00' ],
                        [7,'1','03/04/2015','EUR 14.050917779273' ]  ];
  
    
     /** @test */
    public function getReportCommand()
    {
        $this->assertTrue(class_exists(\App\Console\Commands\GetClientReport::class));
    }
    
    /** @test */
    public function itCanGetReport()
    {
        $this->artisan('get:report "1"')->expectsTable($this->tableHeader,$this->rows)
        ->assertExitCode(0);
        $this->assertTrue(true);


    }
    
  
}

第一个测试 (getReportCommand()) 有效,测试通过。 第二个没有,给我这个错误:

⨯ it can get report

  ---

  • Tests\Unit\GetReportTest > it can get report
  Output "+----+----------+------------+----------------------+" was not printed.

  at tests/Unit/GetReportTest.php:29
     25▕     /** @test */
     26▕     public function itCanGetReport()
     27▕     {
     28▕         $this->artisan('get:report "1"')->expectsTable($this->tableHeader,$this->rows)
  ➜  29▕         ->assertExitCode(0);
     30▕                                 
     31▕                                 
     32▕ 
     33▕ 

我的问题是: 我如何与命令输出交互? 这是执行此类测试的正确方法吗? 我需要一个函数来测试每个可能的输入吗?

【问题讨论】:

    标签: laravel unit-testing testing laravel-artisan


    【解决方案1】:

    错误Output "+----+----------+------------+----------------------+" was not printed. 可能与行的“长度”有关。如果只有一个数据比您的测试长,则第一行将失败。您可以在代码中再次检查吗?即使是空格也可能导致该问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 2019-08-08
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2019-11-18
      相关资源
      最近更新 更多