简体   繁体   English

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

[英]Laravel DatabaseMigrations produces error: Call to a member function call() on null

I'm trying to run DatabaseMigrations on my unit tests, but I get the following error: 我正在尝试在单元测试中运行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

From DatabaseMigrations: 从DatabaseMigrations:

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

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

From ApplicationTrait: 从ApplicationTrait:

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

Any ideas why I'm getting this error? 有任何想法为什么我会收到此错误吗?

I ended up solving this using this code in my TestCase.php file: 我最终在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;
}

Essentially I'm just calling the migration and rollbacks manually. 本质上,我只是手动调用迁移和回滚。 Not sure why it works and the other doesn't. 不知道为什么它起作用,而另一个不起作用。

I think you should modify createApplication method in your TestCase from 我认为您应该从以下位置修改TestCase createApplication方法:

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

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

    return $app;
}

to: 至:

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

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

    $this->app = $app;  // line added

    return $app;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 调用成员 function getName() on null 错误 - Laravel Call to a member function getName() on null Error 过滤yii2会产生错误在null上调用成员函数isAttributeRequired() - Filtering yii2 produces an error Call to a member function isAttributeRequired () on null Laravel在null上调用成员函数 - Laravel call to member function on null Laravel 5.5-“在null上调用成员函数sync()”错误 - Laravel 5.5 - “Call to a member function sync() on null” Error laravel 5.7错误:在null上调用成员函数save() - laravel 5.7 Error: Call to a member function save() on null 在Laravel中上传文件时出错:在null上调用成员函数 - Error uploading file in Laravel: Call to a member function on null Laravel crud,更新错误(调用成员 function update() on null) - Laravel crud, update error (Call to a member function update() on null) 致电成员 function update() on null Laravel ZC1C425268E68385D1AB5074F17A - Call to a member function update() on null Laravel function Laravel 5 查询关系导致“调用成员函数 addEagerConstraints() on null”错误 - Laravel 5 Querying with relations causes "Call to a member function addEagerConstraints() on null" error Laravel 中间件错误 - 在 null 上调用成员函数 isBasic() - Laravel Middleware Error - Call to a member function isBasic() on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM