PHPUnit 6.2致命错误:Class' Eloquent'在/.../Database/Model.php中找不到

时间:2017-07-28 18:27:05

标签: laravel unit-testing phpunit

在PHPUnit 5.x中我没有遇到任何问题。一旦我升级到6.2,我就开始收到以下错误: Fatal error: Class 'Eloquent' not found in /my/app/Database/Model.php on line 14 我已经执行了composer dump-autoload。因为它之前有效,我认为是因为新版本的变化应该如何设置TestCase.php,但我找不到任何可靠的东西。

在测试中,我正在调用像这样的父设置方法

测试设置方法:

public function setUp()
    {
        parent::setUp();
    }

TestCase.php:

class TestCase extends Illuminate\Foundation\Testing\TestCase {
    protected $baseUrl = 'http://whatever.thing';
    public $user;
    public $password;
    public $mockUser;
    public function createApplication() {
        //Server address used by the test suite's instance of $_SERVER.
        $_SERVER['REMOTE_ADDR'] = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';

        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

}

1 个答案:

答案 0 :(得分:2)

我弄清楚升级过程中出了什么问题。首先,我有一个扩展Eloquent的模型类:

use Eloquent;

我将其更改为此并且有效:

use Illuminate\Database\Eloquent\Model as Eloquent;

然后,我创建了一个新的Laravel 5.4项目,并研究了他们如何实现新版本的PHPUnit。我遵循了新的结构,并将我的TestCase.php更改为新的前进方式。对于任何寻找的人:

TestCase.php:

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

CreatesApplication.php:

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

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

        return $app;
    }
}