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

强颜欢笑 提交于 2019-12-02 08:34:51

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)

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