是否可以在单元测试的验证异常内打印错误?

问题描述

使用单元测试对API进行测试时,是否可以从错误信息包中打印出消息?是的,它已经显示了异常,但没有确切说明错误的发生位置和验证方式。

为清楚起见,请查看以下内容

Tests:  1 Failed,23 passed,3 pending
   Illuminate\Validation\ValidationException
   The given data was invalid

以及我的期望:

Tests:  1 Failed,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);
}

解决方法

您可以UserData.id,这是验证失败时laravel响应的状态代码。

422无法处理的实体