gpt4 book ai didi

scala - 测试重定向后加载的页面

转载 作者:行者123 更新时间:2023-12-01 09:59:33 24 4
gpt4 key购买 nike

我有一个测试用例,用于验证在 POST 调用后,用户被重定向到正确的页面。

"Redirect Page" in {
running(FakeApplication()) {
val Some(result) = route(FakeRequest(POST, "/product/add/something")
.withFormUrlEncodedBody(
"Id" -> "666",
)
.withSession("email" -> "User")
)
status(result) must equalTo(SEE_OTHER)
// contentAsString(result) at this point is just blank

这将验证是否提供了重定向 URL。然后如何让单元测试转到重定向的 URL,以便我可以验证其内容?

最佳答案

您可以测试重定向到的 URL:

    redirectLocation(result) must beSome.which(_ == "/product/666")

如果要检查内容,请按照重定向操作:
    val nextUrl = redirectLocation(result) match {
case Some(s: String) => s
case _ => ""
}
nextUrl must contain("/product/666")

val newResult = route(FakeRequest(GET, nextUrl)).get

status(newResult) must equalTo(OK)
contentType(newResult) must beSome.which(_ == "text/html")
contentAsString(newResult) must contain("something")

关于scala - 测试重定向后加载的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18263971/

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