jkstallard
jkstallard

Reputation: 395

How do I resolve error Call to undefined method assertSessionHasErrors in laravel unit tests?

Using Laravel 8, I'm running some unit tests, but getting this error on one of my tests:

Call to undefined method Tests\Unit\ApplicationTest::assertSessionHasErrors()
use Tests\TestCase;
class ApplicationTest extends TestCase {
...
    $applicationRef = Application::inRandomOrder()->pluck('reference')->first();
    $this->post(
        'http://website.test/applications/'.$applicationRef.'/update',
        ['title' => null]
    );

    $this->assertSessionHasErrors('title');
}

I've tried all sorts to test form validation, but cant get any to work. any advice would be appreciated. thanks

Upvotes: 0

Views: 1449

Answers (1)

matiaslauriti
matiaslauriti

Reputation: 8082

You have to store the result of $this->post in a variable and use that.

$applicationRef = Application::inRandomOrder()->pluck('reference')->first();
$response = $this->post(
    'http://website.test/applications/'.$applicationRef.'/update',
    ['title' => null]
);

$response->assertSessionHasErrors('title');

Upvotes: 2

Related Questions