【发布时间】:2018-04-08 23:51:01
【问题描述】:
我正在尝试编写一个单元测试来检查使用 base_path() 助手的属性方法,但是,我遇到了一个异常:Call to undefined method Illuminate\Container\Container::basePath()。
完整的堆栈跟踪如下:
\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:179
\app\Message.php:47
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:432
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:333
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:306
\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1279
\tests\Unit\MessageTest.php:59
我已经追踪到使用 setUp 和 tearDown 固定装置 - 即使我有:
public function setUp()
{
//$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . "/../..";
}
public function tearDown()
{
//unset($_SERVER['DOCUMENT_ROOT']);
}
我收到了上述错误。如果我完全移除固定装置,错误就会消失。
用setUpBeforeClass 和tearDownAfterClass 替换给定方法后,错误消失了,但我想知道是什么原因造成的。
据我所知,这是 vanilla Laravel 5.4 安装(确切地说是 5.4.36),但它安装了其他库(我真的不能说是什么库)。我还没有设置phpunit.xml 文件,但公平地说,我不知道要查找什么。
我已经用全新安装的 Laravel(相同版本)对其进行了测试,它确实是开箱即用的(使用未修改的 phpunit.xml 文件);在 5.5 版上没有。
【问题讨论】:
标签: laravel unit-testing phpunit laravel-5.4