"the expression is unused" 在 intellij 中使用 kotest 时
"the expression is unused" when using kotest in intellij
对于使用 Kotest 编写的这个 kotlin 测试,IntelliJ 显示警告“表达式未使用”并且语法着色不起作用。
还有,当运行测试时,找不到测试。
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5"
{
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}
只需将左花括号放在“add 3 to 2 should give 5”之后,如下所示:
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5" {
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}
@Thomas Martin 的回答是正确的,但你问的是为什么不同。
Kotest 中的 SpringSpec
依赖于一些 Kotlin DSL 功能来工作。让我们从头开始构建相同的函数来进行演示。
我们将从使用 Kotlin 的扩展函数开始。这允许我们向我们无法控制的 classes 添加功能。
所以,
fun String.test(test: () -> Unit)
那么我们可以在任何字符串上调用这个测试函数:
"this is a test".test({ 1 + 2 shouldBe 3 })
其次,Kotlin 允许将任何最终的 lambda arg 带到括号之外。所以foo(arg, arg2, arg3, lambda)
可以变成foo(arg, arg2, arg3) lambda
。这意味着我们可以将测试写成:
"this is a test".test { 1 + 2 shouldBe 3 }
接下来,我们可以将函数标记为中缀,这样我们就不需要使用点来调用它们了。所以我们的测试函数变成:
infix fun String.test(test: () -> Unit)
我们的测试现在看起来像:
"this is a test" test { 1 + 2 shouldBe 3 }
最后,任何名为 invoke
并标记为运算符函数的函数都可以在没有函数名称的情况下调用。因此 class Foo 中的 operator fun invoke(a: String, b: String)
可以作为常规 Foo.invoke(a,b)
或只是 Foo(a, b)
.
调用
因此,将所有这些放在一起,我们最终的测试函数如下所示:
operator fun String.invoke(test: () -> Unit)
我们的测试结果为:
"this is a test" { 1 + 2 shouldBe 3 }
或者更有可能,
"this is a test" {
1 + 2 shouldBe 3
}
如果将大括号移到下一行,就像您原来的问题一样,它看起来就像一个字符串,后跟一个不相关的 lambda 块。两种不同的表达方式。
对于使用 Kotest 编写的这个 kotlin 测试,IntelliJ 显示警告“表达式未使用”并且语法着色不起作用。 还有,当运行测试时,找不到测试。
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5"
{
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}
只需将左花括号放在“add 3 to 2 should give 5”之后,如下所示:
class TalentMatchServiceSpec : StringSpec() {
init {
"add 3 to 2 should give 5" {
// Given integers 2 and 3
val two = 2
val three = 3
// When adding both numbers
val sum = two + three
// Then the result is 5
sum shouldBe 5
}
}
}
@Thomas Martin 的回答是正确的,但你问的是为什么不同。
Kotest 中的 SpringSpec
依赖于一些 Kotlin DSL 功能来工作。让我们从头开始构建相同的函数来进行演示。
我们将从使用 Kotlin 的扩展函数开始。这允许我们向我们无法控制的 classes 添加功能。
所以,
fun String.test(test: () -> Unit)
那么我们可以在任何字符串上调用这个测试函数:
"this is a test".test({ 1 + 2 shouldBe 3 })
其次,Kotlin 允许将任何最终的 lambda arg 带到括号之外。所以foo(arg, arg2, arg3, lambda)
可以变成foo(arg, arg2, arg3) lambda
。这意味着我们可以将测试写成:
"this is a test".test { 1 + 2 shouldBe 3 }
接下来,我们可以将函数标记为中缀,这样我们就不需要使用点来调用它们了。所以我们的测试函数变成:
infix fun String.test(test: () -> Unit)
我们的测试现在看起来像:
"this is a test" test { 1 + 2 shouldBe 3 }
最后,任何名为 invoke
并标记为运算符函数的函数都可以在没有函数名称的情况下调用。因此 class Foo 中的 operator fun invoke(a: String, b: String)
可以作为常规 Foo.invoke(a,b)
或只是 Foo(a, b)
.
因此,将所有这些放在一起,我们最终的测试函数如下所示:
operator fun String.invoke(test: () -> Unit)
我们的测试结果为:
"this is a test" { 1 + 2 shouldBe 3 }
或者更有可能,
"this is a test" {
1 + 2 shouldBe 3
}
如果将大括号移到下一行,就像您原来的问题一样,它看起来就像一个字符串,后跟一个不相关的 lambda 块。两种不同的表达方式。