I have a controller that validates some input:
public function postLogin(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
}
In my view I have the following piece of code to display these errors:
@if (count($errors) > 0)
<div class="row">
<div class=" alert alert-danger col-sm-offset-2 col-sm-10">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
</div>
@endif
and I have a test that verifies this to be happening:
/** @test */
public function it_validates_login() {
$this->visit('/login')
->press('Login')
->see('The password field is required')
->see('The email field is required');
}
This is all very straight forward and was working as expected. The test was green and everyone was happy.
However, since updating my code to use the "web" middleware group that is provided in Laravel 5.2 on all routes the test is failing. When I try this myself in the browser it still works (as it should) though, so this has to be something specific to do with testing. PHPUnit tells me Failed asserting that the page contains the HTML [The password field is required]. Please check the content above., and indeed, the errors are not in the HTML code PHPUnit is comparing to.
I have tried disabling the ShareErrorsFromSession middleware in the web middleware group, but that actually gave me more errors.
The more I started messing with this the less sense it made.
I'm now under the impression that these errors shouldn't even work without using the web middleware group, but they do. And when I enable the web middleware group but delete all middleware classes that are bound to it in kernel.php it doesn't work and throws an error saying Undefined variable: errors. What's the difference between having no classes in there or just not using it?
Anyway, the issue being: Validation works like a charm, but the logical testing procedures provided by Laravel don't seem to work for verifiying this behavior?!