How to handle Action which returns Accumultor[ByteString,Result] in unit test

我的梦境 提交于 2019-11-27 08:51:55

问题


My Action returns Accumulator[ByteString,Result]. I want to unit test the Accumulator. How can I test it? I am trying to use contentAsJson which accepts a variable of type Accumulator[ByteString,Result] but the Right side of Either is not giving me the content. The following is the test case.

  "newQuestion" should {
    "should return error if tag information in the question isn't in correct format" in {
      val testEnv = new QuestionsControllerSpecTestEnv(components=components)
      val body =
        s"""
          |{
          | "practice-question":{
          | "description": "some description",
          | "hints": ["hint1","hint2"],
          | "image": ["image1 data","image2 data"],
          | "success-test": "success test",
          | "fail-test": "fail test",
          | "tags": ["tag1-in-incorrect-format","tag2IsAlsoWrong"],
          | "title":"some title",
          | "answer": "some answer",
          | "references":["ref1","ref2"]
          | }
          |}
        """.stripMargin

      val jsonBody = Json.parse(body)

      val request = new FakeRequest(FakeRequest("POST","ws/questions/new-question")).withAuthenticator(testEnv.testEnv.loginInfo)(testEnv.testEnv.fakeEnv).withBody(AnyContentAsJson(jsonBody))
      val response = testEnv.questionsController.newQuestion(request)
      val responseBody = contentAsJson(response)//(Timeout(Duration(5000,"millis")),testEnv.testEnv.mat)
      println(s"received response body ${responseBody}")
      val result = (responseBody \ "result").get.as[String]
      val additionalInfo = (responseBody \ "additional-info").get.as[String]
      result mustBe "error"
      additionalInfo mustBe components.messagesApi("error.invalidTagStructure")(components.langs.availables(0))
    }
  }

The controller is receiving a body of type Right(AnyContentAsRaw(RawBuffer(inMemory=0, backedByTemporaryFile=null)))

Why am I not seeing the JSON in the body?


回答1:


I need to call run method of the Accumulator to start the stream which will pass data to the Accumulator.

run method has 3 variants.

abstract def run(elem: E)(implicit materializer: Materializer): Future[A]
Run this accumulator by feeding a single element into it.

abstract def run()(implicit materializer: Materializer): Future[A]
Run this accumulator by feeding nothing into it.

abstract def run(source: Source[E, _])(implicit materializer: Materializer): Future[A]
Run this accumulator by feeding in the given source.

The E seems to be the data type of stream. In my case, Accumulator[-E,+A] has E equal to ByteStream. So I converted the string body into Bytestream and pass it to run. run returns Future[Result] which could then be processed usingcontentAsJsonmethod ofHelpers` class

val body =
        s"""
           |{
           | "practice-question":{
           | "description": "some description",
           | "hints": ["hint1","hint2"],
           | "image": ["image1 data","image2 data","image3 data"],
           | "success-test": "success test",
           | "fail-test": "fail test",
           | "tags": ["${testEnv.tag}","${testEnv.tag}"],
           | "title":"some title",
           | "answer": "some answer",
           | "references":["ref1","ref2"]
           | }
           |}
        """.stripMargin

      val jsonBody = Json.parse(body)

      println("jsBody is "+jsonBody)
...


val request = new FakeRequest(FakeRequest("POST","ws/questions/new-question")).withAuthenticator(testEnv.testEnv.loginInfo)(testEnv.testEnv.fakeEnv).withHeaders(CONTENT_TYPE->"application/json").withBody(AnyContentAsJson(jsonBody))
      val response = testEnv.questionsController.newQuestion(request)
      val accumulatorResult = response.run(ByteString(body)) //use run to start the Accumulator
      val responseBody = contentAsJson(accumulatorResult)//(Timeout(Duration(5000,"millis")),testEnv.testEnv.mat)

..


来源:https://stackoverflow.com/questions/57596864/how-to-handle-action-which-returns-accumultorbytestring-result-in-unit-test

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