0

使用单元测试测试 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);
}
4

1 回答 1

-1

当验证失败时,您可以->assertStatus(422)使用 laravel 响应的状态码。

422 无法处理的实体

于 2020-08-30T09:09:11.550 回答