haakym
haakym

Reputation: 12358

Laravel testing assertions against session and response

When testing a route in Laravel I cannot seem to assert against both the returned response and the session. Is there any reason for this and should I instead be splitting the test into two?

Here's my test, simplified:

$response = $this->call('POST', route('some-route'), $data);

This passes:

$this->assertResponseStatus(302);

This doesn't pass:

$this
    ->assertResponseStatus(302)
    ->assertSessionHasErrors([
        'application_status' => 'Application status has changed. Action not applied.'
    ]);

The test will throw up an error saying it can't assert against null.

I've tried moving the order of the tests round and also assigning the response and session to variables before asserting like so:

$response = $this->call('POST', route('admin.application.action.store'), $data);
$sessionErrors = session()->get('errors');

$this
    ->assertEquals(302, $response->status())
    ->assertTrue($sessionErrors->has('application_status'));

But still get the same issue. Error: Call to a member function assertTrue() on null

Any advice would be greatly appreciated. Thanks!

Upvotes: 0

Views: 3884

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

Assertions don't implement fluid interface. Run it as 2 sequential statements:

$this->assertResponseStatus(302);
$this->assertSessionHasErrors([
    'application_status' => 'Application status has changed. Action not applied.'
]);

Upvotes: 1

Related Questions