我有一个测试用例,应该验证在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,以便我可以验证其内容?
答案 0 :(得分:6)
您可以使用以下命令测试重定向到的网址:
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")