如何测试一个接受请求的json主体和一个没有任何主体的请求的控制器

时间:2019-04-24 21:31:30

标签: playframework-2.6

我的controller的定义如下:

def getQuestion = silhouette.UserAwareAction.async(parse.json){//anyone (logged in or not) can see a question
    implicit request => {...}
  }

它需要一个json主体。如果请求不包含任何正文,我想测试其行为。

以下是我编写的测试用例

  "mycontroller" should {
    "should return error if json body is missing in the request" in {
      val testEnv = new TestEnv(components = components)
     val request = FakeRequest("POST","ws/questions/get-question")

      val responseFuture:Future[Result] = testEnv.myController.getQuestion(request)
      val responseBody = contentAsJson(responseFuture)
      println(s"received response ${responseBody}")
      val result = (responseBody \ "result").get.as[String]
      val additionalInfo = (responseBody \ "additional-info").get.as[String]

      result mustBe "error"

    }
  }

但是我遇到了以下错误

Error:(181, 92) type mismatch;
 found   : play.api.test.FakeRequest[play.api.mvc.AnyContentAsEmpty.type]
 required: play.api.mvc.Request[play.api.libs.json.JsValue]
      val responseFuture:Future[Result] = questionsTestEnv.questionsController.getQuestion(request)

如何将没有任何消息正文的请求传递给控制器​​?

0 个答案:

没有答案