Erik
02/15/2025, 10:55 PMtest("foo") {
Arb.bind<Set<Int>>().filter { it.isNotEmpty() }.checkAll { set ->
val value = set.random()
val newSet = set - value
value shouldNotBeIn newSet
}
}
It fails with: Caused by java.lang.AssertionError: Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.
There are two issues with this exception (1️⃣ and 2️⃣ below)
I really intend to test that the value is not in the set (the business logic under test being set - value
). Is there a fundamental reason that I don't know about that makes testing content on an empty collection nonsensical? I really want to test that the new set does not contain the value.
1️⃣ Looking at the implementation of shouldNotBeIn
, it basically negates the shouldBeIn
matcher. For the latter I understand the error: testing an element being in an empty collection makes no sense. However, testing an element not being in an empty collection could, or even should (?), always pass. Why not?
2️⃣ The error message assumes that I'm testing for emptiness, because it suggest to use Collection.shouldBeEmpty()
. This is not what I'm testing. Therefore, the message isn't helpful.LeoColman
02/16/2025, 12:16 AMEmil Kantis
02/16/2025, 1:41 AMArb.set(<http://Arb.int|Arb.int>(), size = 1).checkAll { set ->
(set - set.single()).shouldBeEmpty()
}
Arb.set(<http://Arb.int|Arb.int>(), 2..10).checkAll { set ->
val value = set.random()
val newSet = set - value
value shouldNotBeIn newSet
}
Erik
02/17/2025, 6:34 PM