laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

assertJsonValidationErrors doesn't work #1154

Closed j-dexx closed 3 years ago

j-dexx commented 3 years ago

Description:

$response->assertJsonValidationErrors('field'); doesn't work in lumen. I think this is because laravel wraps the errors in an errors key on the response for a validation error and lumen doesn't.

Steps To Reproduce:

Have any test that assertsJsonValidationErrors. I'm not sure if this is just intended not to be supported by Lumen but it's a really useful feature I use a lot in laravel.

driesvints commented 3 years ago

Please provide all the code needed to reproduce this.

j-dexx commented 3 years ago

@driesvints - https://github.com/j-dexx/json_validation_errors/blob/main/tests/ExampleTest.php

Only things I've done are change the test, added a route and added an action on the example controller

$response->assertJsonValidationErrors('name');

So the response body is

{
  "name": ["The name field is required."]
}

But assertJsonValidationErrors by default expects the validation to be in a key of errors in the response.

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Testing/TestResponse.php#L636 I assume (potentially wrongly lumen inherits the test response from laravel somehow)

Just thought about it and I can change the assertion to $response->assertJsonValidationErrors('name', null);

and it'll pass.

Not really sure what the best thing to do is. I guess one of:

driesvints commented 3 years ago

Just thought about it and I can change the assertion to response->assertJsonValidationErrors('name', null); and it'll pass.

I think this is the best way to go here. Feel free to send a PR to the docs, thanks 👍