import io.kotest.core.spec.style.FreeSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
fun foo(i: Int) = i + 1
class `this works ok`: FreeSpec({
withData(1, 2, 3) { i ->
foo(i) shouldBe i + 1
}
})
class `this doesn't work`: FreeSpec({
"some context" {
withData(1, 2, 3) { i ->
foo(i) shouldBe i + 1
}
}
})
The second test class fails with
io.kotest.core.spec.InvalidDslException: Cannot add a root test after the spec has been instantiated: 1
e
Emil Kantis
06/19/2023, 5:12 PM
You can't define tests with
withData
inside a leaf, only at root level or inside a container.
k
Klitos Kyriacou
06/19/2023, 5:20 PM
Ah, I see, thanks. It feels a little counterintuitive that when not using
withData
, the assertions must be inside a leaf (which may optionally be inside a container), but when using
withData
the assertions must be directly under a container.
e
Emil Kantis
06/19/2023, 5:24 PM
withData
generates a leaf for each row under the hood, so they're not in a container. But I understand that it can be perceived as being directly under the container