【问题标题】:Missing TestCase::artisan() in Laravel 6.x Command TestLaravel 6.x 命令测试中缺少 TestCase::artisan()
【发布时间】:2019-12-31 14:05:21
【问题描述】:

我在docs 之后创建了一个非常基本的控制台命令测试:

<?php

namespace Tests\Feature;

use PHPUnit\Framework\TestCase;

class QueueJobCommandTest extends TestCase
{


    /**
     * Test a job argument is requied
     *
     * @return void
     */
    public function testNoArgumentsIsError()
    {
        $this->artisan('queue:job')
            ->expectsOutput('No job specified')
            ->assertExitCode(0);
    }
}

但是当我运行 phpunit 时出现错误:

Error: Call to undefined method Tests\Feature\QueueJobCommandTest::artisan()

任何关于为什么 TestCase::artisan() 未定义的帮助将不胜感激。

【问题讨论】:

    标签: php laravel phpunit


    【解决方案1】:

    扩展 Laravel 版本的 TestCase

    use Tests\TestCase;
    

    希望对你有帮助

    【讨论】:

    • 刚刚意识到这一点.. 谢谢!我想需要一杯咖啡。
    【解决方案2】:

    您必须从 Laravel 扩展 TestCase,其中包括所有 Laravel 功能。在这一点上,文档真的很好。

    https://laravel.com/docs/5.8/testing

    <?php
    
    namespace Tests\Unit;
    
    use Tests\TestCase;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    
    class ExampleTest extends TestCase
    {
        /**
         * A basic test example.
         *
         * @return void
         */
        public function testBasicTest()
        {
            $this->assertTrue(true);
        }
    }
    

    这应该可以解决您的问题。有时我会创建一个类,我可以在其中添加一些特殊的身份验证功能,例如从 Laravel TestCase 类扩展的类扩展。然后您可以在该类中添加您的自定义函数。

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2023-04-02
      • 2019-11-13
      • 2020-01-06
      • 1970-01-01
      • 2014-03-21
      • 2018-10-19
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多