Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Display unexpected validation errors when asserting status #38046

Merged
merged 3 commits into from
Jul 18, 2021
Merged

[8.x] Display unexpected validation errors when asserting status #38046

merged 3 commits into from
Jul 18, 2021

Conversation

jessarcher
Copy link
Member

This PR expands on #38025 to also show any unexpected validation errors that occurred when asserting a status code other than 422.

Currently the only way to see the validation errors is by dumping the response, running the test again, and then removing the dump. With this PR, the unexpected validation errors will be displayed straight away.

If the response being asserted against is JSON, the validation errors will be also be displayed as JSON. Otherwise the errors will be displayed as a simple list.

@taylorotwell taylorotwell merged commit c5ee631 into laravel:8.x Jul 18, 2021
@paulredmond
Copy link
Contributor

paulredmond commented Jul 20, 2021

Hi @jessarcher & @taylorotwell!

I've been experimenting with this feature in an example app, and in theory the code works (i.e. tests pass), but in userland tests I don't believe $this->exceptions->last() will ever return a ValidationException instance because it's in the $internalDontReport array in the base Handler class.

This means that ValidationException instances will never be pushed to the LoggedExceptionCollection instance during tests.

A fresh Laravel 8.51 app shows the following error without the expected validation messages:

image

I am not sure off the top of my head how to get around this, but sure enough this feature works when I remove ValidationException from the $internalDontReport array:

image

@@ -165,7 +166,7 @@ public function assertStatus($status)
{
$actual = $this->getStatusCode();

$lastException = $actual === 500 && $status !== 500
$lastException = $actual !== $status && in_array($actual, [422, 500])
? $this->exceptions->last()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always return null in the case of ValidationException since it's part of the base Handler internal don't report array:

protected $internalDontReport = [
AuthenticationException::class,
AuthorizationException::class,
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
MultipleRecordsFoundException::class,
RecordsNotFoundException::class,
SuspiciousOperationException::class,
TokenMismatchException::class,
ValidationException::class,
];

@jessarcher
Copy link
Member Author

I've been experimenting with this feature in an example app, and in theory the code works (i.e. tests pass), but in userland tests I don't believe $this->exceptions->last() will ever return a ValidationException instance because it's in the $internalDontReport array in the base Handler class.

Crap. Thanks for catching this! I'm working on a solution that checks the response for session errors and errors in the content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants