eit*_*hed 1 phpunit unit-testing laravel laravel-5.4
我正在尝试编写一个单元测试来检查使用base_path()helper 的属性方法,但是,我收到一个异常: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
Run Code Online (Sandbox Code Playgroud)
我已经追踪到使用setUp和tearDown固定装置 - 即使我有:
public function setUp()
{
//$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . "/../..";
}
public function tearDown()
{
//unset($_SERVER['DOCUMENT_ROOT']);
}
Run Code Online (Sandbox Code Playgroud)
我收到上述错误。如果我完全移除固定装置,错误就会消失。
我把它换成与给定的方法后setUpBeforeClass和tearDownAfterClass错误消失,但我想知道是什么原因造成的。
据我所知,这是 vanilla Laravel 5.4 安装(完全是 5.4.36),但它安装了额外的库(我真的不能说是什么库)。我还没有设置phpunit.xml文件,但公平地说,我不知道要寻找什么。
我已经用全新安装的 Laravel(相同版本)对其进行了测试,它确实是开箱即用的(带有未改动的 phpunit.xml 文件);在 5.5 版上没有。
小智 9
尝试在您自己的内部调用父类的 setUp 和 tearDown 方法。
像这样:
public function setUp()
{
parent::setUp();
//$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . "/../..";
}
public function tearDown()
{
parent::tearDown();
//unset($_SERVER['DOCUMENT_ROOT']);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6127 次 |
| 最近记录: |