如何在单元测试中处理 returns Accumultor[ByteString,Result] 的 Action

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

我的ActionreturnsAccumulator[ByteString,Result]。我想对 Accumulator 进行单元测试。我怎样才能测试它?我正在尝试使用 contentAsJson,它接受类型为 Accumulator[ByteString,Result] 的变量,但是 Either 的 Right 端没有给我内容。以下是测试用例。

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

控制器正在接收 Right(AnyContentAsRaw(RawBuffer(inMemory=0, backedByTemporaryFile=null)))

类型的正文

为什么我在正文中看不到 JSON?

我需要调用 Accumulatorrun 方法来启动将数据传递到累加器的流。

run 方法有 3 个变体。

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.

E好像是stream的数据类型。在我的例子中,Accumulator[-E,+A] 的 E 等于 ByteStream。所以我把字符串body转成Bytestream传给runrun 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)

..