我正在使用 Play 2.2 和 ScalaTest。这是我第一次尝试在我的游戏应用程序中测试 Controller 。我这样做:
class TestArtistController extends PlaySpec with OneAppPerSuite {
"ArtistController" must {
"returns artists" in new WithApplication {
val eventuallyResult = controllers.ArtistController.findNearCity("lyon")(FakeRequest())
whenReady(eventuallyResult, timeout(Span(2, Seconds))) { result =>
println(result.body) //it is an Enumerator[Array[Byte]]
result.header.status mustBe 200
}
}
}
}
它允许我正确测试返回结果,但我不知道如何测试结果的主体。 result.body
返回一个 Enumerator[Array[bytes]]
,我完全不知道如何转换它以检索 Json
我正在发送。
执行此操作的好方法是什么?
最佳答案
导入 play.api.test.Helpers._
并使用辅助函数,例如 contentAsJson(res)
或 status(res)
, res
是您操作的结果(在某些情况下,您可能需要使用 res.run
来执行异步操作)。
关于scala - 在 Play 中使用 ScalaTest 对 Controller 进行单元测试!框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30549751/