I have a problem in a StringSpec using withData: `...
# kotest
h
I have a problem in a StringSpec using withData:
Copy code
"should drop default port" {
            withData(
                "<http://hei:80>" to "<http://hei>",
                "<http://hei:88>" to "<http://hei:88>",
                "<https://hei:443>" to "<https://hei>",
                "<https://hei:448>" to "<https://hei:448>",
            ) { (input, expected) ->
                input.toUrl().toString() shouldBe expected
                input.toUri().toString() shouldBe expected
            }
        }
This gives me
Copy code
Cannot add a root test after the spec has been instantiated: (<http://hei:80>, <http://hei>)
io.kotest.core.spec.InvalidDslException: Cannot add a root test after the spec has been instantiated: (<http://hei:80>, <http://hei>)
	at io.kotest.core.spec.DslDrivenSpec.add(DslDrivenSpec.kt:46)
	at io.kotest.core.spec.style.scopes.RootScopeKt.addTest(RootScope.kt:33)
	at io.kotest.datatest.RootKt.withData(root.kt:65)
	at io.kotest.datatest.RootKt.withData(root.kt:51)
	at io.kotest.datatest.RootKt.withData(root.kt:16)
Any ideas what I'm doing wrong?
Seems like it's not possible. I'll roll my own with listof(...).forEach(...) instead. It would have been a nice feature to have!
k
You can use
FreeSpec
at a container level (using
-
):
Copy code
class MySampleDataDrivenTest : FreeSpec({
       "should drop default port" - {
            withData(
                "<http://hei:80>" to "<http://hei>",
                "<http://hei:88>" to "<http://hei:88>",
                "<https://hei:443>" to "<https://hei>",
                "<https://hei:448>" to "<https://hei:448>",
            ) { (input, expected) ->
                input.toUrl().toString() shouldBe expected
                input.toUri().toString() shouldBe expected
            }
        }
})
h
-
!
interesting 🙂
thanks!
Would have been nice if this was mentioned on https://kotest.io/docs/framework/datatesting/data-driven-testing.html
k
To be fair, it says "data tests can be nested inside any number of containers. But this is optional, you can define data tests at the root level as well." However, I agree that it's not immediately obvious, and I've found that the terminology is not entirely clear (e.g. it took me some time before I understood the term "container").
h
I realize I still don't understand kotest's definition of a container, although I haven't really tried that hard. I'll try to improve!