How do I use specs2 to confirm a date is equal to another date if their difference is less than 1 second?
I am writing a specs2 test and I need to compare two dates:
import java.util.Date
val date1: Date = getDate1();
val date2: Date = getDate2();
date1 must beEqualToAnotherDate(date2, 1.second)
There is no such routine beEqualToAnotherDate
, how can I do the same in specs2 efficiently?
+3
source to share
2 answers
If you need a custom connector, you can try something like:
import java.util.Date import org.specs2.matcher.{ Expectable, Matcher, MatchResult, MatchersImplicits, Specification } object MySpecs extends Specification with MatchersImplicits { def beCloseInTimeTo(date: Date, timeDiff: Int) = new Matcher[Date] { def apply[D <: Date](e: Expectable[D]) = result((e.value.getTime - date.getTime) < timeDiff, "Dates are nearly at the same time", "Dates are different", e) } dateA must beCloseInTimeTo(dateB, timeInMillis) }
+3
source to share