Hello everyone! I have the following data structur...
# kotest
p
Hello everyone! I have the following data structure:
Copy code
data 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:
Copy code
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!
t
I might miss something as well, but I usually with
Copy code
user should {
  check one by one
}
an alternative is
withConstantNow
Copy code
"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)
p
I’m unfortunately only using the kotest assertions - but this is again an example of why I should introduce the testing framework also to our team. 😁 I didn’t want to do something like
Copy code
user.asClue {
  ..comparing all properties..
}
because if I add a property later on it’ll not be reflected in the tests.
t
there are ways around the localdate issue. usually you want to have a
Clock
object, that is an interface and you mock it as well
Copy code
"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 anyway
l
There is that matcher exactly, isn't it?
https://kotest.io/docs/assertions/core-matchers.html "Field by Field Comparison Matchers"
and Selective Matchers
Or do you mean only LocalDateTime field specifically?