2017-10-06 121 views
1

我想爲我的控制器方法設置一個簡單的單元測試。Laravel 5.5單元測試控制器方法

目標是測試視圖是否具有預期值。

/** 
* Does the homepage receive all companies when there is no licensekey provided. 
* 
* @return void 
*/ 
public function testAllCompaniesOnHomepageWithoutLicensekey() 
{ 
    $this->call('GET', '/'); 

    $allCompanies = Company::all(); 

    $this->assertViewHas('allCompanies', $allCompanies); 
} 

在我conosle我得到以下錯誤:

Error: Call to undefined method Tests\Unit\ExampleTest::assertViewHas()

我不知道這是否是不再availbale在Laravel 5.5?

任何人都知道我可以測試我的目標嗎?

+0

@Troyer我不明白爲什麼我會用黃昏這一點。我不想做browsertests。 – Chris

回答

2

您是否正在從較舊的Laravel版本遷移?已經有變化Laravel的瀏覽器測試中Laravel 5.4 https://laravel.com/docs/5.4/upgrade

在Laravel 5.5,你可以嘗試:

$response = $this->get('/'); 

$allCompanies = Company::all(); 
$response->assertViewHas('allCompanies', $allCompanies); 
+1

啊我試着調試'get_class_methods($ this)',但是你贏了...(Y) –

+0

我是從舊的Laravel版本遷移過來的。 – Chris