【问题标题】:Why doesn't PHPUnit see Laravel Facades?为什么 PHPUnit 看不到 Laravel Facades?
【发布时间】:2020-08-10 02:43:00
【问题描述】:

我对 PHPUnit 测试和 Laravel 有疑问 例外:找不到类“DB”

我的测试用例文件:

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

我的 CreatesApplication 文件:

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

这是我的测试:

namespace Tests\Unit;

use App\Services\RepetitionService;
use PHPUnit\Framework\TestCase;

class RepetitionServiceTest extends TestCase
{
   public function testGetNextIteration()
   {
       $repetitionService = new RepetitionService;

       $nextIteration = $repetitionService->getNextIteration(1);
       $this->assertEquals(2, $nextIteration);
   }
}

正在测试的方法:

public function getNextIteration(int $lastIteration) : int
    {
        // some example code (method is not real but code below copied from original method)
        \DB::table('cards')->get();
        Config::get('params.repeat_iterations');
        config('params.repeat_iterations');
    }

表扬: php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml tests/Unit

版本: 拉拉维尔 7 PHP 7.4 PHPUnit 9

【问题讨论】:

  • 试试use Illuminate\Support\Facades\DB; 和刚才的DB::table(...)

标签: laravel phpunit


【解决方案1】:

尝试从此路径使用数据库

use Illuminate\Support\Facades\DB;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2020-07-12
    • 2023-03-31
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多