alexcanana's avatar

Testing flash messages and redirects

Hello,

I wrote the following test but it's not working :

 $this->followingRedirects()
    ->post('/contact-me', $attributes)
    ->assertSessionHas('contact.success')
    ->assertStatus(200)
    ->assertSee('confirm');

It returns :

Session is missing expected key [contact.success].
Failed asserting that false is true.

Obviously it's because of $this->followingRedirects().

Does anyone have a solution?

Thanks in advance,

Alex

0 likes
5 replies
Nash's avatar

Do you really need all 3 assertions? Is the session key the main thing you are trying to test or would it be enough to check the HTTP status code (200 OK as opposed to some error code) + assertSee? Is there a situation where the session key is NOT set but the rest of the action would be successful and return 200?

alexcanana's avatar

You're right, I don't really need all these assertions.

Since the confirmation page can't be displayed if this session variable isn't there, I just wanted to make it clear for my future self. And I have other tests for that so I guess that's OK.

I always have some trouble defining what I need to assert, and always end up doing too much.

But still, I'm curious, would it be possible ?

josegus's avatar

The test you mention interacts with objects, not with assertSession method from phpunit

Please or to participate in this conversation.