我正在尝试使用 Play Framework 2.0 和 Specs2 测试模型方法。Global.scala 在第一次运行时用数据填充数据库。在一项测试中,我可以使用如下代码成功测试它:
def dateHelper(str: String): Date = new SimpleDateFormat("MM/dd/yyyy").parse(str)
"Food model" should {
"be retrieved by id" in {
val Some(mashedPotatoes) = Food.findById(1000)
mashedPotatoes.name must equalTo("Mashed Potatoes")
mashedPotatoes.eaten must equalTo(false)
mashedPotatoes.id must equalTo(Id(1000))
mashedPotatoes.owner must equalTo(Id(1))
mashedPotatoes.expiry must equalTo(dateHelper("05/21/2012"))
}
}
那个测试通过了。但是,如果我尝试从模型中选择多个项目,并将其作为列表进行测试:
"return food for test user in " in {
running(FakeApplication()) {
val testFoods: Seq[Food] = Food.findFoodFor(Id(1)) // Test user's ID is 1
// This test fails
testFoods must equalTo(
List(
Food(Id(1001), "Fried Green Tomatoes", false, Id(1), dateHelper("04/21/2012")),
Food(Id(1000), "Mashed Potatoes", false, Id(1), dateHelper("05/21/2012"))
)
)
// This test passes
testFoods.head.expiry must equalTo(dateHelper("04/21/2012"))
}
}
错误输出告诉我日期字段不相等:
[error] x return food for test user in
[error] 'Food(1001,Fried Green Tomatoes,false,1,2012-04-21 00:00:00.0), Food(1000,Mashed Potatoes,false,1,2012-05-21 00:00:00.0)' is not equal to 'Food(1001,Fried Green Tomatoes,false,1,Sat Apr 21 00:00:00 EDT 2012), Food(1000,Mashed Potatoes,false,1,Mon May 21 00:00:00 EDT 2012)' (ModelSpec.scala:66)
[error] Expected: ...se,1,[Sat Apr ]21...00[ EDT 2]0[12]),...1,[Mon May ]21...00[ EDT 2]0[12])
[error] Actual: ...se,1,[2012-04-]21...00[.]0[]),...1,[2012-05-]21...00[.]0[])
这里有什么我想念的吗?
编辑:看起来它是数据库模式,它将到期列设置为时间戳类型,而不是日期类型。
更多有用的信息在这里:java.util.Date vs java.sql.Date