【问题标题】:Kotest: PythagTriple example failing with erroneous function callKotest:PythagTriple 示例因函数调用错误而失败
【发布时间】:2022-11-22 03:15:03
【问题描述】:

我正在使用 IntelliJ 和 Maven,下载了 Kotest 插件,并将依赖项添加到 pom.xml(kotest-runner-junit5-jvm,kotest-assertions-core-jvm,kotest-property-jvm,所有版本 5.5。 0).

以下基本示例正在运行:

class MyFirstTestClass : FunSpec({
    test("my first test") {
        1 + 2 shouldBe 3
    }
})

但我不能让另一个例子起作用,PythagTriple:

import io.kotest.core.spec.style.FunSpec
import io.kotest.core.spec.style.StringSpec
import io.kotest.data.forAll
import io.kotest.matchers.shouldBe

data class PythagoreanTriple(
    val a: Int,
    val b: Int,
    val c: Int
)

class MyTests : FunSpec({
    context("Pythagorean triples tests") {
        forAll(
            PythagoreanTriple(3, 4, 5),
            PythagoreanTriple(6, 8, 10),
            PythagoreanTriple(8, 15, 17),
            PythagoreanTriple(7, 24, 25)
        ) { (a, b, c) ->
            isPythagoreanTriple(a, b, c) shouldBe true
        }
    }
})

fun isPythagoreanTriple(a: Int, b: Int, c: Int): Boolean = a * a + b * b == c * c

我可以找到此示例的两种变体,一种使用 forAll,另一种使用 withData。两者都不起作用。

似乎有两个问题:

(1)

Kotlin: None of the following functions can be called with the arguments supplied: 
public suspend fun <A> forAll(vararg rows: Row1<TypeVariable(A)>, testfn: suspend (TypeVariable(A)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B> forAll(vararg rows: Row2<TypeVariable(A), TypeVariable(B)>, testfn: suspend (TypeVariable(A), TypeVariable(B)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C> forAll(vararg rows: Row3<TypeVariable(A), TypeVariable(B), TypeVariable(C)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D> forAll(vararg rows: Row4<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E> forAll(vararg rows: Row5<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F> forAll(vararg rows: Row6<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G> forAll(vararg rows: Row7<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G, H> forAll(vararg rows: Row8<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G, H, I> forAll(vararg rows: Row9<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H), TypeVariable(I)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H), TypeVariable(I)) -> Unit): Unit defined in io.kotest.data

使用 withData 只会导致 Unresolved reference: withData,而且我还没有找到导入。

(2)

Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.

这似乎指的是以下内容:

        { **(a, b, c)** ->
            isPythagoreanTriple(a, b, c) shouldBe true
        }

考虑到我是新手,这些一定是我无法解决的一些基本问题。任何帮助将不胜感激。

【问题讨论】:

    标签: maven kotest


    【解决方案1】:

    有 3 个错误使您的测试不起作用:

    1. 您的测试设置不包含测试,仅包含上下文。这可以简单地通过将context 更改为test 来解决。
    2. 你不应该为forAll行使用你自己的数据类型,但正如 (1) 中的消息所暗示的,forAll 的不同变体是为数据类型Row1Row2、@定义的987654327@,等等。它们中的每一个都可以用row(...)创建。 (从 1 到 14 的每个元数都有一个 row 函数。)
    3. forEach的变体不是指定一个带有一个RowX 参数的函数,但是一个带有X 参数类型的函数是RowX 的参数类型。因此,参数 a, b, c 被括号括起来是不正确的——这是将一个参数解构为其组件的一种表示法。

      当您按以下方式更改测试时,您的测试总的来说是有效的:

      class PythagoreanTripleTest : FunSpec({
          test("Pythagorean triples tests") {
              forAll(
                  row(3, 4, 5),
                  row(6, 8, 10),
                  row(8, 15, 17),
                  row(7, 24, 25)
              ) { a, b, c ->
                  isPythagoreanTriple(a, b, c) shouldBe true
              }
          }
      })
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多