Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Laravel PHPUnit测试,使用seePageIs()的奇怪行为_Unit Testing_Laravel_Phpunit - Fatal编程技术网

Unit testing Laravel PHPUnit测试,使用seePageIs()的奇怪行为

Unit testing Laravel PHPUnit测试,使用seePageIs()的奇怪行为,unit-testing,laravel,phpunit,Unit Testing,Laravel,Phpunit,试图掌握测试的窍门。我有一个相当简单的测试,只是为了确保用户在进入根页面时被踢到登录: public function index_without_auth_should_redirect_to_login() { $response = $this->get('/'); $response->seeStatusCode(302); $response->seePageIs('/login'); } 这一直持续到

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

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()
方法。它的响应是200(而您的响应是302)

您需要首先按照重定向进行操作,或者验证
位置
标题,以查看您是否被重定向到登录页面

您可以将一个断言与
MakesHttpRequests
trait一起使用:

$this->get('/'))
$this->see状态码(302);
$this->assertRedirectedTo('http://localhost/login');

谢谢!这是有道理的。有没有线索说明为什么headers道具在5.2中不起作用?我在文档中找不到任何内容:“Undefined属性:…AuthControllerTest::$headers”它不是测试属性,而是响应属性。我假设您的$request是request的一个实例。不是吗?不,我一直在尝试通过测试注入请求,但我没有任何运气。get()方法看起来像什么?抱歉,我被变量名弄糊涂了。这在技术上不是一个请求。我更新了我的答案。希望这有帮助!