[英]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.