首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >describe子句中的kotest嵌套规范

describe子句中的kotest嵌套规范
EN

Stack Overflow用户
提问于 2020-05-27 18:56:53
回答 2查看 830关注 0票数 2

我已经开始使用kotest:4.0.5 (kotlintest),但在describe子句中嵌套的stringSpec函数出现了问题。

示例:

代码语言:javascript
复制
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子句之外,则可以正常工作。

你知道如何在子句中嵌套规范/函数吗?

EN

回答 2

Stack Overflow用户

发布于 2020-05-28 04:28:48

您只能在顶级使用include。这是工厂测试( include关键字的用途)实现方式的一部分(可能在将来的版本中会有所放宽)。

不过,你可以把整个东西搬到工厂去。

代码语言:javascript
复制
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) }
                }
            }
        }
    }
}

您可以根据需要对命名进行参数化,因为这只是字符串,例如:

代码语言:javascript
复制
fun emailValidation(name: String) = describeSpec {
    describe("Registration") {
        context("$name") {
        }
    }
}

如果您没有参数化,那么使用测试工厂就没有什么意义了。只需声明测试内联IMO即可。

票数 2
EN

Stack Overflow用户

发布于 2021-04-28 18:25:37

对于嵌套的include,您可以实现自己的工厂方法,如下例所示:

代码语言:javascript
复制
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" { }
}

注意传递给containerTemplatetestCaseTemplate扩展函数的Scope

输出:

代码语言:javascript
复制
Scenario: root container
   template container with FreeScope context
       nested template testcase with FreeScope context
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62041457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档