单元测试 Laravel 包时未找到配置类

Jul*_*ien 3 php phpunit unit-testing package laravel-5

我正在开发 Laravel (5.4) 包,并且正在尝试进行单元测试。我有这门课:

<?php

namespace Sample;

class Foo
{
    public function getConfig()
    {
        $config = \Config::get('test');

        return $config;
    }   
}
Run Code Online (Sandbox Code Playgroud)

我有这个测试:

<?php

use PHPUnit\Framework\TestCase;
use Sample\Foo;

class FooTest extends TestCase
{
    public function testGetConfig()
    {
        $foo = new Foo;
        $config = $foo->getConfig();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我执行 phpunit 时出现此错误:

错误:未找到“配置”类

我如何对这个类进行单元测试?

谢谢。

小智 5

PHPUnit\Framework\TestCase您应该扩展而不是扩展Tests\TestCase

<?php
namespace Tests\Unit;

// use PHPUnit\Framework\TestCase;
use Tests\TestCase;
use Sample\Foo;

class FooTest extends TestCase
{
    public function testGetConfig()
    {
        $foo = new Foo;
        $config = $foo->getConfig();
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,Config或者其他 Laravel 外观可能无法在方法中工作@dataProvider,请参阅PHPUnit 数据提供程序中不可用的 Laravel 框架类以获取更多信息。