Play Framework 2.0 및 Specs2를 사용하여 모델 메서드를 테스트하려고합니다. Global.scala는 처음 실행될 때 DB로 데이터를 채 웁니다. 한 테스트에서 아래 코드와 같이 성공적으로 테스트 할 수 있습니다.java.util.Date 객체가 하나의 사양 테스트를 충족하지만 다른 테스트가 실패하는 이유는 무엇입니까?
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[])
인가가 내가 놓친 게있어?
편집 : 만료 열을 date 유형 대신 timestamp 유형으로 설정 한 DB 스키마 인 것처럼 보입니다.
더 도움이 보이는 여기 정보 : java.util.Date vs java.sql.Date
다음과 같은 경우 : testFoods.head는 equalToTo (Food (Id (1001), "Fried Green Tomatoes", false, Id (1), dateHelper ("04/21/2012")))입니다. 그것도 실패합니까? – Christian
실제로 뭔가 빠졌습니다. Joda Time ;-) 문제가되지는 않지만 Scala와 같은 언어에서는 불변성을 조장합니다. Joda Time은 SimpleDateFormat보다 적합합니다. 문제가 무엇이든, 나는 SDF가 범죄자인지 아닌지 궁금하다 ... – virtualeyes