我已经开始使用kotest:4.0.5 (kotlintest),但在describe子句中嵌套的stringSpec函数出现了问题。
示例:
class SellerTest : DescribeSpec({
describe("Registration") {
context("Not existing user") {
include(emailValidation()
}
}
})
fun emailValidation() = stringSpec {
"Email validation" {
forAll(
row("test.com"),
row("123123123123123")
) { email ->
assertSoftly {
val exception =
shouldThrow<ServiceException> { Email(email) }
}
}
}
}如果include(emailValidation())在describe子句之外,则可以正常工作。
你知道如何在子句中嵌套规范/函数吗?
发布于 2020-05-28 04:28:48
您只能在顶级使用include。这是工厂测试( include关键字的用途)实现方式的一部分(可能在将来的版本中会有所放宽)。
不过,你可以把整个东西搬到工厂去。
class SellerTest : DescribeSpec({
include(emailValidation)
})
val emailValidation = describeSpec {
describe("Registration") {
context("Not existing user") {
forAll(
row("test.com"),
row("123123123123123")
) { email ->
assertSoftly {
val exception =
shouldThrow<ServiceException> { Email(email) }
}
}
}
}
}您可以根据需要对命名进行参数化,因为这只是字符串,例如:
fun emailValidation(name: String) = describeSpec {
describe("Registration") {
context("$name") {
}
}
}如果您没有参数化,那么使用测试工厂就没有什么意义了。只需声明测试内联IMO即可。
发布于 2021-04-28 18:25:37
对于嵌套的include,您可以实现自己的工厂方法,如下例所示:
class FactorySpec : FreeSpec() {
init {
"Scenario: root container" - {
containerTemplate()
}
}
}
/** Add [TestType.Container] by scope function extension */
suspend inline fun FreeScope.containerTemplate(): Unit {
"template container with FreeScope context" - {
testCaseTemplate()
}
}
/** Add [TestType.Test] by scope function extension */
suspend inline fun FreeScope.testCaseTemplate(): Unit {
"nested template testcase with FreeScope context" { }
}注意传递给containerTemplate和testCaseTemplate扩展函数的Scope
输出:
Scenario: root container
template container with FreeScope context
nested template testcase with FreeScope contexthttps://stackoverflow.com/questions/62041457
复制相似问题