我陷入了以下问题:我想编写一个 specs2 规范来断言我的到和从 json 转换是对称的。但是,我在 joda datetime 日期上遇到错误。
'2012-04-17T00:04:00.000+02:00' is not equal to '2012-04-17T00:04:00.000+02:00'. Values have the same string representation but possibly different types like List[Int] and List[String] (TimeSpecs.scala:18)
这是一个证明问题的极简规格
import org.joda.time.DateTime
import org.specs2.mutable.Specification
class TimeSpecs extends Specification {
"joda and specs2" should {
"play nice" in {
val date = DateTime.parse("2012-04-17T00:04:00+0200")
val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
date === date2
}
"play nice through play json transform" in {
import play.api.libs.json._
import play.api.libs.json.Json._
val date = DateTime.parse("2012-04-17T00:04:00+0200")
val jsDate= toJson(date)
val date2= jsDate.as[DateTime]
date === date2
}
}
}
我应该如何在第二次测试中比较 date 和 date2 ?它们是相同的,但 specs2 似乎没有看到:(
- - 编辑
“手动”在运行时使用 date.getClass.getCanonicalName 检查类型按预期返回 org.joda.time.Datetime
import org.joda.time.DateTime
import org.specs2.mutable.Specification
class TimeSpecs extends Specification {
"joda and specs2" should {
"play nice" in {
val date = DateTime.parse("2012-04-17T00:04:00+0200")
val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
date === date2
}
"play nice through play json transform" in {
import play.api.libs.json._
import play.api.libs.json.Json._
val date:DateTime = DateTime.parse("2012-04-17T00:04:00+0200")
val jsDate= toJson(date)
val date2:DateTim= jsDate.as[DateTime]
println(date.getClass.getCanonicalName) //prints org.joda.time.DateTime
println(date2.getClass.getCanonicalName)//prints org.joda.time.DateTime
date === date2
}
}
}
使用 DateTime#isEqual 确实有效,但我失去了流畅匹配器的好处以及它们带来的有用错误消息。此外,我实际上要比较的是恰好包含日期的案例类实例,而不是日期本身。
使用
date should beEqualTo(date2)
产生相同的错误===