Philipp Mayer
09/29/2021, 8:51 AMdata class User(
val id: String,
val someTime: LocalDateTime,
val someDetails: SomeDetails,
) {
data class SomeDetails(
val otherId: String,
val otherTime: LocalDateTime,
)
}
val user = User(
id = "1",
someTime = LocalDateTime.now(),
someDetails = User.SomeDetails(
otherId = "a",
otherTime = LocalDateTime.now().plusSeconds(1)
)
)
val otherUser = User(
id = "1",
someTime = LocalDateTime.now().plusHours(1),
someDetails = User.SomeDetails(
otherId = "a",
otherTime = LocalDateTime.now().plusSeconds(22)
)
)
I would like to write a test that ignores both LocalDateTime
fields.
Something in the lines of:
class SomeTest {
@Test
fun `should ignore localdatetime fields`() {
user.shouldBeEqualToIgnoringFields(otherUser, User::someTime, User.SomeDetails::otherTime)
}
}
Is there any matcher that I might have missed when going through the docs?
Thanks in advance!thanksforallthefish
09/29/2021, 11:25 AMuser should {
check one by one
}
an alternative is withConstantNow
"test" {
withConstantNow(LocalDateTime.now()) {
user shouldBe otherUser
}
}
withConstantNow
fixes a point in time and all LocalDateTime.now
would return always the same value (so no need to ignore those anymore)Philipp Mayer
09/29/2021, 11:31 AMuser.asClue {
..comparing all properties..
}
because if I add a property later on it’ll not be reflected in the tests.thanksforallthefish
09/29/2021, 12:25 PMClock
object, that is an interface and you mock it as well
"test" {
User(Clock.fixed("fixed time"))
}
data class User(private val clock: Clock) {
val date = LocalDateTime.now(clock)
}
in general using a clock is suggested anywayLeoColman
09/30/2021, 6:36 PM