gpt4 book ai didi

php - 单元测试 laravel 包时找不到配置类

转载 作者:行者123 更新时间:2023-12-05 04:11:42 24 4
gpt4 key购买 nike

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

<?php

namespace Sample;

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

return $config;
}
}

我有这个测试:

<?php

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

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

当我执行 phpunit 时出现这个错误:

Error: Class 'Config' not found

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

谢谢。

最佳答案

您应该扩展Tests\TestCase,而不是扩展PHPUnit\Framework\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();
}
}

此外,Config 或其他 Laravel 外观可能无法在 @dataProvider 方法中工作,请参阅 Laravel framework classes not available in PHPUnit data provider了解更多信息。

关于php - 单元测试 laravel 包时找不到配置类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com