Kirill Zhukov
09/18/2024, 11:35 PMasClue
doesn't print the clue in this example?
test("foo") {
"some clue".asClue {
emptyList<Unit>().single()
}
}
stacktrace:
List is empty.
java.util.NoSuchElementException: List is empty.
at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:616)
Lidonis Calhau
09/18/2024, 11:42 PMemptyList<Unit>().shouldNotBeEmpty()
should display the clueKirill Zhukov
09/18/2024, 11:51 PMsingle()
throws NPE or IllegalArgumentException
and not the AssertionFailedError
? Might explain why half of our test failures do not include clues - quite a lot of our testing infrastructure code and logic doesn't use kotest assertions because it doesn't need to and it's easier that way. Unfortunately that clue doesn't get printed in these cases.Kirill Zhukov
09/18/2024, 11:54 PMsingle { someCondition }
quite frequently instead of shouldHaveSingleElement { }
in our tests. single
returns the item and that's what we need in most of our tests. Whereas shouldHaveSingleElement
returns the subjected list...Kirill Zhukov
09/18/2024, 11:59 PMKirill Zhukov
09/19/2024, 12:04 AMAssertionFailedError
in meantime 🤔Oliver.O
09/19/2024, 9:43 AMKirill Zhukov
09/19/2024, 4:17 PMHowever, I'd advise to stick with using only Assertion-type exceptions for test failures, as that's part of the informal protocol.I think from our experience using assertion APIs only is limiting and creates very verbose code - often it's a lot easier to just call
single { someCondition }
like in this example instead of calling an extra shouldHaveSingleElement
beforehand. Maybe I'm missing some special way on how to write this code differently?
rows // List<RowModel>
.single { it.title = "Row 1" }
.onClick()
Kirill Zhukov
09/19/2024, 4:20 PMrows // List<RowModel>
.shouldHaveSingleElement { it.title == "Row 1" }
.single { it.title = "Row 1" } // should never throw at this point but we just the matched row
.onClick()
maybe I could reuse the predicate, but this is quite verbose, given the simplicity of the example. 🤔Emil Kantis
09/20/2024, 11:22 AMrows // List<RowModel>
.forSingle { it.title == "Row 1" }
.onClick()
should work, forSingle
tests for the lambda and then (if a single match is found) returns the resultOliver.O
09/20/2024, 11:54 AMforSingle
expects a Kotest assertion inside its lambda, right? So it would be
rows // List<RowModel>
.forSingle { it.title shouldBe "Row 1" }
.onClick()
Emil Kantis
09/20/2024, 11:55 AM