Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jimmerioles's avatar

assertSessionHasErrors() not detecting 'errors' in Session

I am trying to test my register page for validation error messages when no inputs are supplied.

/** @test */
    public function it_redirects_back_with_errors_when_no_inputs()
    {
        $this->visit('/register')
            ->press('Register')
            ->seePageIs('/register');
       ->assertSessionHasErrors(['username']);
       //->assertSessionHasErrors('username'); same result
       //->assertSessionHasErrors(['username' => 'The username field is required.']); same result
      //->assertSessionHasErrors(); i also tried this but same result
    }

i always recieve this :

Session missing key: errors
Failed asserting that false is true.
 /home/vagrant/embark/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:89
 /home/vagrant/embark/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:121
 /home/vagrant/embark/tests/acceptance/RegisterPageTest.php:87

I can verify that there are errors in my session, i used dd(Session::all()) in my views and also substituted ->see('The username field is required.'); which all confirmed that there are errors data in my session. Hope somebody could help :(

0 likes
1 reply
Valorin's avatar

Just stumbled upon this issue, came looking, and found no responses to this... so I figured out a solution.

There are no errors in session as the helpers used follow redirects and render the page, which clears the errors. You can either look for the content of the validation errors in the page, or you can use the call() or route() helpers to make the request. They don't follow redirects, so the errors are still in the session:

$user = factory(\App\User::class)->make();

$this->actingAs($user);
$this->route('post', 'rulers.create');
$this->assertRedirectedToRoute('rulers.create');
$this->assertSessionHasErrors();
2 likes

Please or to participate in this conversation.