kotest-assertions-core / io.kotest.matchers.equality / shouldBeEqualToIgnoringFields

shouldBeEqualToIgnoringFields

fun <T : Any> T.shouldBeEqualToIgnoringFields(other: T, vararg properties: KProperty<*>): Unit

Asserts that this is equal to other without using specific fields

Verifies that this instance is equal to other without using some specific fields. This is useful for matching on objects that contain unknown values, such as a database Entity that contains an ID (you don't know this ID, and it doesn't matter for you, for example)

Opposite of shouldNotBeEqualToIgnoringFields

Example:

data class Foo(val id: Int, val description: String)

val firstFoo = Foo(1, "Bar!")
val secondFoo = Foo(2, "Bar!")

firstFoo.shouldBeEqualToIgnoringFields(secondFoo, Foo::id) // Assertion passes

firstFoo shouldBe secondFoo // Assertion fails, `equals` is false!

Note: Throws IllegalArgumentException in case properties parameter is not provided.