我正在尝试测试一个尝试解析请求中发送的JSON的控制器方法:
def addRoomToProfileForTime = Action.async(parse.json[AddRoomToProfileForTimeRequest]) { request =>
profileService.addRoomToProfileForTime(request.body.roomId, request.body.profileId, request.body.timeRange).value.map {
case Xor.Right(_) => Ok
case Xor.Left(err) => BadRequest(Json.toJson(err))
}
}
Run Code Online (Sandbox Code Playgroud)
这是表示请求的案例类:
final case class AddRoomToProfileForTimeRequest(
roomId: Int,
profileId: Int,
timeRange: TimeRange
)
implicit val addRoomToProfileForTimeRequestFormat:Format[AddRoomToProfileForTimeRequest] = Json.format
Run Code Online (Sandbox Code Playgroud)
这个代码按预期工作,我发出这样的请求:
curl -H "Content-Type: application/json" -X POST -d '{"roomId":3,"profileId":1,"timeRange":{"id":1,"fromTime":"2000-01-01T01:01","toTime":"2000-01-01T02:01"}}' http://localhost:9000/api/profiles/addRoomToProfileForTime
Run Code Online (Sandbox Code Playgroud)
但我正在尝试为这种方法编写一个测试(注意我使用macwire进行依赖注入,因此不能使用WithApplication:
"add a room to profile for time" in new TestContext {
val roomId = 1
val profileId = 1
val from = "2000-01-01T01:01"
val to = "2000-01-01T02:01"
val …Run Code Online (Sandbox Code Playgroud)