【发布时间】:2018-07-08 01:13:08
【问题描述】:
我是 PHPUnit 和 TDD 的新手。我只是将我的项目从 Laravel 5.4 升级到 5.5 并安装了 phpunit 6.5.5。在学习过程中,我写了这个测试:
/** @test */
public function it_assigns_an_employee_to_a_group() {
$group = factory(Group::class)->create();
$employee = factory(Employee::class)->create();
$this->post(route('employee.manage.group', $employee), [
'groups' => [$group->id]
]);
$this->assertEquals(1, $employee->groups);
}
我在 web.php 文件中有一个定义的路由,看起来像这样
Route::post('{employee}/manage/groups', 'ManageEmployeeController@group')
->name('employee.manage.group');
我还没有创建ManageEmployeeController,当我运行测试时,我收到了这个错误,而不是告诉我控制器不存在的错误
断言 null 匹配预期 1 失败。
请问我该如何解决这个问题?
【问题讨论】:
-
在测试中你想断言 1 等于某事,然后它说它不等于!如果你没有断言路由存在,你为什么期望呢?
-
我需要使用
$this->withoutExceptionHandling()来触发错误。您的回答对我找到问题的答案很有帮助。
标签: laravel unit-testing phpunit laravel-5.4 laravel-5.5