我正在 play_2.9.1-2.0.3 上构建一个应用程序并使用specs2_2.9.1-1.7.1(与 play 捆绑在一起)进行测试。我有一个看起来像这样的动作:
def createPoll() = Action { request =>
    request.body.asJson.map {jsonBod =>
        Poll.fromJsonValue(jsonBod).fold(
            thrown => CommonResponses.invalidCreatePollRequest(this),
            poll => (CommonResponses.createdPoll(poll, this))
    )}.getOrElse(CommonResponses.expectingJson(this))
}
当我从 curl 向它发送消息时,这可以按预期工作,但是在我的 specs2 测试中,我得到了这个异常:
ClassCastException: java.lang.String cannot be cast to play.api.mvc.AnyContent(PollController.scala:16)
其中第 16 行是:
def createPoll() = Action { request =>
这是测试的相关部分:
    routeAndCall(
        FakeRequest(
            PUT, controllers.routes.PollController.createPoll().url,
            FakeHeaders(),
            "{\"userId\":\"%s\",\"title\":\"%s\"}".format(userId, title)
        ).withHeaders(("Content-type", "application/json"))
    )
如果我将createPolldef 更改为:def createPoll() = Action(parse.tolerantText) {
然后我可以通过 specs2 测试使其工作。
有谁知道我做错了什么?理想情况下,我想使用 parse.json 正文解析器,但我希望能够使用规范而不仅仅是 curl 进行测试。谢谢