2015-12-09 53 views
0

我想在我的單元測試運行DatabaseMigrations調用一個成員函數調用(),但我得到了以下錯誤:Laravel 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); 
} 

任何想法,爲什麼我得到這個錯誤?

+0

顯示「VisitStaffPagesTest」類的代碼 –

+0

這裏是:http://kopy.io/TZn9o –

+0

你在哪裏定義了'createApplication'方法? –

回答

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; 
} 

基本上我只是手動調用遷移和回滾。不知道它爲什麼會起作用,其他不起作用。

+0

這一個爲我工作真棒:) –

0

我想你應該從

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

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

    return $app; 
} 

修改createApplication方法在TestCase

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

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

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

    return $app; 
} 
+0

我編輯它,但我有同樣的錯誤:'錯誤:調用成員函數調用()null' –