https://kotlinlang.org logo
#kotest
Title
# kotest
l

Lukasz Kalnik

12/28/2021, 6:14 PM
How do you assert matchers on nested object contents? Something like capturing slots in MockK. E.g. I have an object like this:
Copy code
Success(Either.Left(IOError(cause=com.google.gson.stream.MalformedJsonException)))
I want to assert certain things: • that the contents of
Success
is of type
Left
• that the contents of
Left
is of type
IOError
• that the
cause
is of type
MalformedJsonException
s

sam

12/28/2021, 6:23 PM
Are you using arrow ?
l

Lukasz Kalnik

12/28/2021, 6:25 PM
Yes
s

sam

12/28/2021, 6:25 PM
And Success here is a kotlin Result ?
l

Lukasz Kalnik

12/28/2021, 6:25 PM
No
It's a custom class from Retrofit I guess
But my question is more general
s

sam

12/28/2021, 6:26 PM
ok, well for things like Arrow, we offer matchers that will smart cast as well as return the contained value
l

Lukasz Kalnik

12/28/2021, 6:27 PM
What about the `IOError`'s
cause
property type?
s

sam

12/28/2021, 6:27 PM
Copy code
val v = either.shouldBeLeft()
v shouldBe "whatever"
or
Copy code
either.shouldBeLeft().shouldBe("whatever")
👍 1
l

Lukasz Kalnik

12/28/2021, 6:27 PM
Can I somehow extract it?
And assert that its type is
MalformedJsonException
?
I don't want to check equality, I just want to check the type of the
cause
property
s

sam

12/28/2021, 6:28 PM
either.shouldBeLeft().cause.shouldBeInstanceOf<MalformedJsonException>()
l

Lukasz Kalnik

12/28/2021, 6:28 PM
ah ok, thanks!
14 Views