【问题标题】:Laravel DatabaseMigrations produces error: Call to a member function call() on nullLaravel DatabaseMigrations 产生错误:在 null 上调用成员函数 call()
【发布时间】:2015-12-09 21:18:17
【问题描述】:

我正在尝试在我的单元测试中运行 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);
}

知道为什么我会收到此错误吗?

【问题讨论】:

  • 显示你的VisitStaffPagesTest类的代码
  • 这里是:kopy.io/TZn9o
  • 你在哪里定义了createApplication方法?
  • TestCase 中的内容:kopy.io/MfiqP
  • 这里的新细节:这只发生在我的 Mac 上,而不是在我的实时服务器 (CentOS) 上。

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


【解决方案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;
}

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

【讨论】:

    【解决方案2】:

    我认为你应该修改 TestCase from 中的 createApplication 方法

    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';
    
        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
    
        return $app;
    }
    

    到:

    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';
    
        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
    
        $this->app = $app;  // line added
    
        return $app;
    }
    

    【讨论】:

    • 我编辑了它,但我有同样的错误:'错误:在 null 上调用成员函数 call()'
    猜你喜欢
    • 2021-11-05
    • 2018-09-06
    • 2020-07-16
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多