测试重定向后加载的页面 [英] Testing a page that is loaded after a redirect
本文介绍了测试重定向后加载的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个测试用例,可以用来验证在POST调用之后,用户是否被重定向到正确的页面.
I've got a test case that is supposed to verify that, after a POST call, the user is redirected to the correct page.
"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,以便可以验证其内容?
This verifies that a redirect URL is given. How do I then get the unit test to go to the redirected URL so that I can verify its content?
推荐答案
您可以使用以下方法测试重定向到的URL:
You can test the URL redirected to with:
redirectLocation(result) must beSome.which(_ == "/product/666")
如果要检查内容,请遵循重定向:
If you want to check the content, follow the redirect:
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")
这篇关于测试重定向后加载的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文