groovy - spock 测试的 where 子句中的多项赋值?

标签 groovy spock

我正在使用 Spock 框架进行测试,我有一个关于 where 子句中的多变量赋值的问题。

我有以下测试用例:

def "sending a message delegates to message sender"() {
  when:
    sendMessage(x,y)
  then:
    1 * messageSender.send(x,y)
  where:
    x << 1
    y << 2
}

我想用单个赋值操作替换 where 子句中的多个变量赋值。我试过:
where:
  [x,y] << [1,2]

但有一个 MissingMethod 异常。我认为这是因为表达式 [1,2] 被视为数组而不是列表。

请注意,以下操作有效:
where:
  [x,y] << [1,2].combinations()

似乎组合()方法返回一个列表类型,但尽管以下不起作用:
where:
  [x,y] << [1,2].asList()

使用组合() 是违反直觉的,所以我想知道是否有一种简单、优雅的方式来初始化 Spock 中的多个变量。

编辑:我使用的是 spock 版本 0.7-groovy-2.0

最佳答案

必须按如下所述进行多项分配:

[x, y] << [[1,2]]

这是一个 contrived example其中一种组合的测试失败。

Multiple variable data pipes使用 SQL ResultSet 中的一个示例,它与上面的示例非常相似。

关于groovy - spock 测试的 where 子句中的多项赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22160165/

相关文章:

Spring Boot - 不再在配置上调用 destroy()

grails - Spock GlobalExtension 未加载 (grails)

spock - 如何使用Spock框架进行参数捕获?

grails - 确定Spock测试的执行顺序

java - 将 JSON 对象从 JsonSlurper 转换回附加的 JSONArray - Groovy

groovy - 使用 Groovy 的 HTTP Builder 使用 PUT 更新 JSON 数据

docker - 从单个 Jenkinsfile 运行多个 Docker 容器

intellij-idea - 如何配置IntelliJ以使用JUnit 4运行测试?

grails - Grails 2.2应该使用什么Spock,Geb和Selenium版本?

grails - 如何在 Grails Geb/Spock 测试用例中获取 sessionFactory?