【问题标题】:Laravel 5.5 Unit testing controller methodLaravel 5.5 单元测试控制器方法
【发布时间】:2017-10-06 10:16:59
【问题描述】:

我正在尝试为我的一个控制器方法设置一个简单的单元测试。

目标是测试视图是否具有预期值。

/**
 * 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);
} 

在我的控制台中,我收到以下错误:

错误:调用未定义的方法 Tests\Unit\ExampleTest::assertViewHas()

我不确定这在 Laravel 5.5 中是否不再可用?

有人知道我可以如何测试我的目标吗?

【问题讨论】:

  • @Troyer 我不明白为什么我会为此使用 Dusk。我不想做浏览器测试。

标签: php laravel unit-testing laravel-5


【解决方案1】:

你是从旧版本的 Laravel 迁移吗? Laravel 5.4 中对 Laravel 的浏览器测试进行了更改https://laravel.com/docs/5.4/upgrade

在 Laravel 5.5 中你可以尝试:

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

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

【讨论】:

  • 啊,我尝试调试get_class_methods($this),但你赢了...(Y)
  • 我正在从旧的 Laravel 版本迁移。