gpt4 book ai didi

laravel - Laravel 测试方法 assertRedirect($uri) 和 assertLocation($uri) 的区别?

转载 作者:行者123 更新时间:2023-12-04 15:09:39 24 4
gpt4 key购买 nike

我正在阅读 Laravel 文档 HTTP tests出现了一个问题。我分不清 assertLocation($uri)assertRedirect($uri) 之间的区别,因为两者都用于重定向到特定的 uri

如能提供帮助,我们将不胜感激。

最佳答案

如果我们看两者的功能assertLocation($uri) 将断言当前位置 header 与给定的 URI 匹配。
但是 assertRedirect($uri) 会断言响应是否重定向到给定的 URI。

I agree with example given by @apokryfos,
only 3xx responses are considered to be redirect responses while a 201 is not a redirect so assertLocation will pass if the response is 201 with a specified location while assertRedirect will not pass for 201 responses.


如果我们从代码的角度来看,
assertRedirect() 函数也在内部调用 assertLocation() 但它还会使用 PHPUnit::assertTrue() 检查响应是否被重定向,如果不那么它会发送消息

'响应状态码[201]不是重定向状态码。',其中201指定响应的状态码。

检查来自 github repo of frameworkassertRedirect()

关于laravel - Laravel 测试方法 assertRedirect($uri) 和 assertLocation($uri) 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65446044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com