如何解决 laravel 单元测试中对未定义方法 assertSessionHasErrors 的错误调用?

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

使用 Laravel 8,我正在 运行 进行一些单元测试,但在我的一项测试中出现此错误:

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');
}

我已经尝试了各种方法来测试表单验证,但无法正常工作。任何意见,将不胜感激。谢谢

您必须将 $this->post 的结果存储在一个变量中并使用它。

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

$response->assertSessionHasErrors('title');