Does anyone have suggestions for efficiently asser...
# arrow
s
Does anyone have suggestions for efficiently asserting that an
Either<Exception, Any>
with
left
equal to a caught
Exception
is equal to an expected value?
s
This is a great option if you don’t mind the Kotest assertion dependency. It allows to rewrite your snippet to:
Copy code
fun doSomething() = IllegalAccessException().left()

    @Test
    fun sample() {
        val actual = doSomething()
        actual.shouldBeLeft().shouldBeTypeOf<IllegalAccessException>
    }
otherwise you could also just do:
Copy code
assert(actual == expected)
Since
Either
is implemented as a
data class
, the problem here is that
Throwable
is not comparable. Every constructed
Throwable
has an unique
StackTrace
and are thus not equal.
assert(RuntimeExeception() != RuntimeExeception())
s
That Kotest extension is perfect, thank you!
👍🏻 1
Ends up
shouldBeLeft()
is a broken assertion. Added an issue about it: https://github.com/kotest/kotest-extensions-arrow/issues/99
s
Ooof, you’re correct. Will create a PR to Kotest to fix asap
s
Glad I could help with identifying it 🙂