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

后端 未结 1 2024
野的像风
野的像风 2020-12-12 06:48

My Action returns Accumulator[ByteString,Result]. I want to unit test the Accumulator. How can I test it? I am trying to use con

相关标签:
1条回答
  • 2020-12-12 07:47

    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)
    
    ..
    
    0 讨论(0)
提交回复
热议问题