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?
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 using
contentAsJsonmethod of
Helpers` 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)
..