kotlin-test:如何测试特定类型,例如:"is y instance of X"
kotlin-test: How to test for a specific type like: "is y instance of X"
如何测试 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}", "")
}
}
在 KotlinTest 中,很多都是关于适当的间距 :)
您可以使用 should
访问各种内置匹配器。
import io.kotlintest.matchers.beInstanceOf
import io.kotlintest.should
value should beInstanceOf<Type>()
还有一个替代语法:
value.shouldBeInstanceOf<Type>()
有关详细信息,请参阅 here。
如何测试 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}", "")
}
}
在 KotlinTest 中,很多都是关于适当的间距 :)
您可以使用 should
访问各种内置匹配器。
import io.kotlintest.matchers.beInstanceOf
import io.kotlintest.should
value should beInstanceOf<Type>()
还有一个替代语法:
value.shouldBeInstanceOf<Type>()
有关详细信息,请参阅 here。