Laravel 测试与 API 开发全解析
1. Laravel 测试方法
1.1 清除输入与上传
$this->clearInputs()
方法用于清除之前设置的所有输入或上传内容。
1.2 工作与事件测试
-
$this->expectsEvents($eventClassName)
:用于断言在测试期间是否触发了特定类的事件。例如:
public function test_usersubscribed_event_fires_when_subscribing()
{
$this->expectsEvents(App\Events\UserSubscribed::class);
$this->visit('subscribe')->type('me@me.com', 'email')->press('Subscribe');
}
-
$this->withoutEvents()
:该方法并非断言,而是禁用 Laravel 的事件处理系统,使测试期间无需担心事件产生的影响,如发送邮件或写入日志。 -
$this->expectsJobs()
:用于断言在测试期间是否触发了特定类的作业。例如: