使用单元测试测试 API 时,是否可以从错误包中打印消息?是的,它已经显示了异常,但它没有具体说明错误是什么以及在哪个验证中。
为了清楚起见,请看一下:
Tests: 1 failed, 23 passed, 3 pending
Illuminate\Validation\ValidationException
The given data was invalid
而我所期待的:
Tests: 1 failed, 23 passed, 3 pending
Illuminate\Validation\ValidationException
The given data was invalid, [
'errors' => [all the validation errors listed here]
]
更新
已经尝试过了,但不起作用:
// inside setUp method
$this->withoutExceptionHandling();
// inside test method
$this->postJson($uri, $this->newUser)
// ->dumpSession()
// ->dumpHeader()
->dump();
另外请看一下我的脚本:
public function testCustomerCanStoreNewUser()
{
$this->actingAs($this->customer, 'api');
$uri = route('users.store');
$this->postJson($uri, $this->newUser)
->dumpSession()
->dumpHeaders()
->dump()
->assertCreated()
->assertJsonStructure(['success', 'data']);
$this->assertDatabaseHas('users', $this->newUser);
}