Testing a page that is loaded after a redirect

北慕城南 提交于 2019-12-22 11:12:05

问题


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

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?


回答1:


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")


来源:https://stackoverflow.com/questions/18263971/testing-a-page-that-is-loaded-after-a-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!