Laravel DatabaseMigrations产生错误:在null上调用成员函数call()

时间:2015-12-09 21:18:17

标签: php laravel laravel-5 phpunit laravel-5.1

我尝试在单元测试中运行DatabaseMigrations,但是我收到以下错误:

1) VisitStaffPagesTest::testLogBills
Error: Call to a member function call() on null

/Users/x/Documents/office/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:312
/Users/x/Documents/office/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:12

从DatabaseMigrations:

public function runDatabaseMigrations()
{
    $this->artisan('migrate'); // This is line 12

    $this->beforeApplicationDestroyed(function () {
        $this->artisan('migrate:rollback');
    });
}

来自ApplicationTrait:

public function artisan($command, $parameters = [])
{
    return $this->code = $this->app['Illuminate\Contracts\Console\Kernel']->call($command, $parameters);
}

为什么我收到此错误的任何想法?

2 个答案:

答案 0 :(得分:1)

我最终在我的TestCase.php文件中使用此代码解决了这个问题:

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

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

    $this->code = $app['Illuminate\Contracts\Console\Kernel']->call('migrate');
    $this->beforeApplicationDestroyed(function () use ($app) {
        $app['Illuminate\Contracts\Console\Kernel']->call('migrate:rollback');
    });

    return $app;
}

基本上我只是手动调用迁移和回滚。不知道为什么它有效,而另一个没有。

答案 1 :(得分:0)

我认为您应该从{/ 1}}

修改createApplication方法
TestCase

为:

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

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

    return $app;
}