PlaySpec 测试通过,即使结果不匹配

PlaySpec test passing even though the result doesn't match

我写了以下规范。令人惊讶的是,即使结果不匹配,它也通过了

代码片段

  val controller = new UserController(mockUserRepository,mockControllerComponents,mockSilhouette)
  //val request = FakeRequest[AnyContentAsJson]("POST", "/ws/users/signup").withJsonBody(Json.parse("""{"bad": "field"}"""))//FakeRequest(POST,"/ws/users/signup").withJsonBody(Json.parse("""{"bad":"field"}"""));
  val request = FakeRequest("POST","ws/users/signup")
  println("sending request",request)
  //val result = controller.someMethod()
  val result = controller.signupUser(request)

  Await.result(result,Duration("10 secs"))
  result.map(response => {
    println("response from controller:"+response)
    response mustBe play.api.mvc.Results.BadRequest
  })

控制台打印 (sending request,POST ws/users/signup)

在控制器中打印。 (received request,POST ws/users/signup)

Controller returns 200OK 但我将其与 BadRequest 匹配。仍然测试通过! response from controller:Result(200, Map())

我怀疑我没有正确匹配结果。我正在做 response mustBe play.api.mvc.Results.BadRequest。我知道 responseResultsBadRequestStatus。但我不知道还有什么可以匹配的,也不知道为什么测试不会失败。我也试过以下,可以看到值不同,但即使这样测试也通过了。

  println("response from controller:"+response.header.status+" compared with "+play.api.mvc.Results.BadRequest.header.status)
        response.header.status mustBe play.api.mvc.Results.BadRequest.header.status

控制台打印 - response from controller:200 compared with 400

像这样导入 Helpers 对象的成员

import play.api.test.Helpers._

允许访问各种实用程序方法以在测试中从 Future[Result] 获得 extract 结果。例如,Helpers.status 可用于提取状态代码,如下所示:

val result: Future[Result] = controller.signupUser(request)
status(result) mustBe BAD_REQUEST

另一种选择是使用 ScalaFutures:

中的 whenReady
val result: Future[Result] = controller.signupUser(request)
whenReady(result) { _.header.status mustBe BAD_REQUEST }

另一种选择是扩展 AsyncWordSpec,然后您可以像这样映射 Future

val result: Future[Result] = controller.signupUser(request)
result.map(_.header.status mustBe BAD_REQUEST)