[9.x] Add new assertJsonValidationErrorRule
testing helper
#41239
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR proposes and adds a new
assertJsonValidationErrorRule
testing helper for HTTP testing.If approved and merged I'll submit a PR for the docs too in the new 2 days 🤞
Why
Currently if you want to test that your endpoint returns a validation error you can use
assertJsonValidationErrorFor
orassertJsonValidationErrors
. The issue with both of them is that you're probably just checking that a validation error is returned, but not what error is returned; this is not the best idea because your test could fail for another reason.If you want to check for a specific error you have to compile the message error yourself (replacing the placeholders) or just harcoding it.
Solution
The new
assertJsonValidationErrorRule
, in conjuction with a newValidator
public method to compile messages, can take care of everything for you: it retrieves the translated message, does the replaces and works with custom rules too.You can search for multiple errors too:
And, of course, you can pass your custom validation rule: