kotlin-test:如何测试特定类型,例如:“是X的y实例”

时间:2019-04-29 11:52:59

标签: kotlin instanceof kotlintest

如何测试val / var是否为预期类型?

在Kotlin测试中是否缺少某些内容,例如:

value shouldBe instanceOf<ExpectedType>()

这是我的实现方式:

inline fun <reified T> instanceOf(): Matcher<Any> {
    return object : Matcher<Any> {
        override fun test(value: Any) =
                Result(value is T, "Expected an instance of type: ${T::class} \n Got: ${value::class}", "")

    }
}

1 个答案:

答案 0 :(得分:3)

在KotlinTest中,很多关于适当的间距:) 您可以使用should来访问各种内置匹配器。

import io.kotlintest.matchers.beInstanceOf
import io.kotlintest.should

value should beInstanceOf<Type>()

还有另一种语法:

value.shouldBeInstanceOf<Type>()

有关更多信息,请参见here