我们如何在 where 块中使用 hashmap 作为变量

How could we use hashmap as a variable in where block

我想在这里放一个SET,怎么办? 我用谷歌搜索 spock where block new HashSet 没有得到任何结果。

    @Unroll
    def "Sample"() {
        expect:
            .....
        where:
        base | exponent || result1 | result2
        1    | 2        || 1 | {{I want to put a SET<ID> here, how?}}

    }

我认为您不仅是 Spock 新手(我在您之前的问题中注意到),而且还是 Groovy 新手。没问题。 :-) 你应该 google 而不是 groovy set literal 并找到类似 this page.

的东西

在 Spock 中,您可以将 where: 块中的变量定义为特征方法(测试方法)的方法参数,包括为它们提供如下类型:

@Unroll
def "sample"(int base, int exponent, int result1, Set<Integer> result2) {
  expect:
  result2 instanceof Set

  where:
  base | exponent || result1 | result2
  1    | 2        || 1       | [1, 2, 3]
}

这会将列表文字转换或强制转换为一个集合。或者您可以节省大量输入,只需使用 Groovy as 运算符,如我链接到的页面所示:

@Unroll
def "sample"() {
  expect:
  result2 instanceof Set

  where:
  base | exponent || result1 | result2
  1    | 2        || 1       | [1, 2, 3] as Set<Integer>
}

您可以使用 Set<Id> 而不是 Set<Integer>,无论您的 Id class 是什么。