【发布时间】:2021-04-16 17:21:49
【问题描述】:
程序中正确抛出异常,但测试未检测到。为什么??
public function null_user_send()
{
$message = 'Hi';
$response = $this->post(route('dialogSend'), ['to' => -1, 'message' => $message]);
$this->assertDatabaseMissing('messages', ['to' => -1, 'message' => $message]);
$this->expectException(Exception::class);
$response->assertStatus(500);
}
邮件控制器:
/** @throws \Exception */
public function dialogSend(Request $request)
{
$handler = app(MailHandler::class);
if ($request->input('to') <= 0) {
throw new \Exception('Параметр "to" имел отрицательное значение', 500);
}
...
}
测试响应:“断言抛出“异常”类型的异常失败。 什么类别的例外都没有关系,没有任何作用。请帮忙
【问题讨论】:
标签: php laravel unit-testing exception