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

laracoft's avatar

Returning, but not getting session errors in a test

// returned by GET /create
return view('pages', $this->data())
    ->withErrors(['reject' => _('reject')]);

...

function test_my_test_method()
{
    $response = $this->call(
            'GET', '/create'
        );

        $this->assertEquals(200, $response->status());
        $response->assertSessionHasNoErrors(); // strangely, this passes
        $response->assertSessionHasErrors(['reject']); // session is missing expected key exception
}

My test is failing because it is not getting the reject key.

Is my code correct? Or am I supposed to do it differently?

0 likes
2 replies
tisuchi's avatar

@laracoft How about this?

/** @test */
    public function it_displays_rejection_error()
    {
        // If necessary, replace `YourRouteName` with the actual name of your route
        $response = $this->get(route('YourRouteName'));

        $response->assertStatus(200);

        // Assuming you want to check that the 'reject' error message is in the session
        $response->assertSessionHasErrors(['reject' => _('reject')]);

        // If you want to assert that the error is shown on the page, 
        // replace `YourErrorMessage` with the actual error message
        $response->assertSeeText('YourErrorMessage');
    }
laracoft's avatar

@tisuchi

  1. I don't think _('reject') is required, assertSessionHasErrors is asking for a list of keys.
  2. See my OP updates, assertSessionHasNoErrors passes......

assertSessionHasErrors

Please or to participate in this conversation.