Laravel PHPUnit 测试,seePageIs() 的奇怪行为

Laravel PHPUnit testing, weird behavior with seePageIs()

正在尝试掌握测试的窍门。我有一个相当简单的测试,只是为了确保用户在进入根页面时被踢登录:

public function index_without_auth_should_redirect_to_login()
    {
        $response = $this->get('/');
        $response->seeStatusCode(302);
        $response->seePageIs('/login');
    }

这一直工作到最后一行,似乎没有检查当前路径,测试本身正在尝试重定向,因为我收到此错误 运行 phpunit:

A request to [http://localhost/login] failed. Received status code [302].

我需要用其他方法检查 url 吗?

从技术上讲,您还没有进入登录页面,因为您没有遵循重定向。您得到的异常是完全有效的。 seePageIs() 方法 asserts if page is loaded. It expects 200 响应(而你有 302)。

您需要先跟随重定向,或验证 Location header 以查看您是否被重定向到登录页面。

您可以使用具有 MakesHttpRequests 特征的 assertRedirectedTo() or assertRedirectedToRoute() 断言之一:

$this->get('/')
$this->seeStatusCode(302);
$this->assertRedirectedTo('http://localhost/login');