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

assertSessionHasErrors doesn't work with named MessageBag (only with "default") #19134

Closed
pl78 opened this issue May 10, 2017 · 2 comments
Closed

Comments

@pl78
Copy link

pl78 commented May 10, 2017

  • Laravel Version: 5.4.21
  • PHP Version: 7.1.1

Description:

When doing test using assertSessionHasErrors method the test will fail if you validate the request using MessageBag with your own name (not "default") even thought the validation rules are set and validation works in browser.

Steps To Reproduce:

In my controller in store method I have:

$this->validateWithBag('answer', request(), [
    'body' => 'required'
]);

When I do assert in my test file like this:

$response->assertSessionHasErrors('body');

it fails with message "Session missing error: body".

It pass when in controller I change to:

$this->validate(request(), [
    'body' => 'required'
]);

I am using named MessageBag as I have two forms on one page with textarea field (both called "body").

@pl78
Copy link
Author

pl78 commented May 11, 2017

As for now I've solved this issue with the below solution but propably there is a better way.

In Illuminate\Foundation\Testing\TestResponse.php I changed the line 544 (in assertSessionHasErrors method) from:

PHPUnit::assertTrue($errors->has($value), "Session missing error: $value");

to:

PHPUnit::assertTrue(collect($errors->getBags())->first()->has($value), "Session missing error: $value");

@themsaid
Copy link
Member

Added this as a feature in a PR for 5.5: #19172

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

No branches or pull requests

2 participants