Hey guys! When using assertion `shouldBeFailure`,...
# kotest
l
Hey guys! When using assertion
shouldBeFailure
, the error message I get is
java.lang.AssertionError: Result should be a failure but was kotlin.Unit
While I actually expected it to be more significant that it's a Success, like:
java.lang.AssertionError: Result should be a failure but was Success(kotlin.Unit)
This is due to the fact that
BeFailure
uses
getOrNull
in the
Result
instead of
toString
. I think it would be better to replace it, but I don't know. What do you think? I can open an issue to discuss this further in the Kotest repository
Copy code
class BeFailure : Matcher<Result<Any?>> {
   override fun test(value: Result<Any?>) = MatcherResult(
      value.isFailure,
      { "Result should be a failure but was ${value.getOrNull()}" },
      { "Result should not be a failure" })
}
commonMain/io/kotest/matchers/result/matchers.kt
m
right, imo I’d also expect Success - the toString() implementation of toString of
Result<T>
is actually printing out
Success(...)
maybe that success should just be a
"Result should be a failure but was $value"
Copy code
@SinceKotlin("1.3")
@JvmInline
public value class Result<out T> @PublishedApi internal constructor(
    @PublishedApi
    internal val value: Any?
) : Serializable {
    // etc...

    /**
     * Returns a string `Success(v)` if this instance represents [success][Result.isSuccess]
     * where `v` is a string representation of the value or a string `Failure(x)` if
     * it is [failure][isFailure] where `x` is a string representation of the exception.
     */
    public override fun toString(): String =
        when (value) {
            is Failure -> value.toString() // "Failure($exception)"
            else -> "Success($value)"
        }