英文:
Spock: how to ignore or skip certian case?
问题
int[] randomOnCondition(int input) {
def output = input == 1 ? Random.newInstance().nextInt() : input
[input, output]
}
def test() {
expect:
randomOnCondition(input) == output as int[]
where:
input || output
2 || [2, 2]
1 || [1, _] //如何匹配结果的一部分并忽略其他部分?
}
updated
def test() {
expect:
Integer[] output = randomOnCondition(input)
output[0] == output0
output[1] == output1
where:
input || output0 | output1
2 || 2 | 2
1 || 1 | _ //对于某些情况,可以跳过断言吗?
}
看起来没有解决方案,我应该将用例拆分成两个。
<details>
<summary>英文:</summary>
With Spock, assume some function that will return unexpectable result on certian condition, how to match part of the result and ignore others?
int[] randomOnCondition(int input) {
def output = input == 1 ? Random.newInstance().nextInt() : input
[input, output]
}
def test() {
expect:
randomOnCondition(input) == output as int[]
where:
input || output
2 || [2, 2]
1 || [1, _] //how to match part of the result and ignore others?
}
updated
def test() {
expect:
Integer[] output = randomOnCondition(input)
output[0] == output0
output[1] == output1
where:
input || output0 | output1
2 || 2 | 2
1 || 1 | _ //for somecase , can it skip assert?
}
Seems no solution, I should split the case into two
</details>
# 答案1
**得分**: 1
这是我在之前评论中解释的示例:
```lang-groovy
package de.scrum_master.stackoverflow.q63355662
import spock.lang.Specification
import spock.lang.Unroll
class SeparateCasesTest extends Specification {
int[] randomOnCondition(int input) {
def output = input % 2 ? Random.newInstance().nextInt() : input
[input, output]
}
@Unroll
def "对于输入 #input 的可预测输出"() {
expect:
randomOnCondition(input) == output
where:
input || output
2 || [2, 2]
4 || [4, 4]
6 || [6, 6]
}
@Unroll
def "对于输入 #input 的部分不可预测输出"() {
expect:
randomOnCondition(input)[0] == firstOutputElement
where:
input || firstOutputElement
1 || 1
3 || 3
5 || 5
}
}
更新: 与您的问题略不相关,但如果输出确实包含输入值,可以简化您的测试如下:
@Unroll
def "对于输入 #input 的可预测输出"() {
expect:
randomOnCondition(input) == [input, input]
where:
input << [2, 4, 6]
}
@Unroll
def "对于输入 #input 的部分不可预测输出"() {
expect:
randomOnCondition(input)[0] == input
where:
input << [1, 3, 5]
}
英文:
Here is an example of what I explained in my previous comment:
package de.scrum_master.stackoverflow.q63355662
import spock.lang.Specification
import spock.lang.Unroll
class SeparateCasesTest extends Specification {
int[] randomOnCondition(int input) {
def output = input % 2 ? Random.newInstance().nextInt() : input
[input, output]
}
@Unroll
def "predictable output for input #input"() {
expect:
randomOnCondition(input) == output
where:
input || output
2 || [2, 2]
4 || [4, 4]
6 || [6, 6]
}
@Unroll
def "partly unpredictable output for input #input"() {
expect:
randomOnCondition(input)[0] == firstOutputElement
where:
input || firstOutputElement
1 || 1
3 || 3
5 || 5
}
}
Update: Somewhat unrelated to your question, but a way to streamline your test if the output really contains input values:
@Unroll
def "predictable output for input #input"() {
expect:
randomOnCondition(input) == [input, input]
where:
input << [2, 4, 6]
}
@Unroll
def "partly unpredictable output for input #input"() {
expect:
randomOnCondition(input)[0] == input
where:
input << [1, 3, 5]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论