2 回答

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
正如 John Camerin 所說(shuō),您可能正在尋找spock 中的數(shù)據(jù)驅(qū)動(dòng)測(cè)試。
我會(huì)提供一個(gè)稍微不同的答案:
def "GetNumberOfMaxHeightCandles"() {
given: "A BirthdayCandles object"
def candles = new BirthdayCandles(testInput)
when: "I call the max number height method"
def actual = candles.getNumberOfMaxHeightCandles()
then: "I should get the frequency count of the max number in the integer array"
actual == expectedResult
where:
testInput | expectedResult
[1,1,1,3,3,3,3] as int [] | 4
[1,1,1,3,3,3,4] as int [] | 1
}
幾個(gè)觀察:
請(qǐng)注意,我沒(méi)有在這里使用字符串插值(沒(méi)有“$result”和“$test”)
請(qǐng)注意as int[]where 塊中的 。它的替代方案是def candles = new BirthdayCandles(testInput as int [])

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以添加一個(gè) where: 塊,其中包含一個(gè)值表,其中第一行是變量名,可以在測(cè)試的其余部分使用。例如
def "GetNumberOfMaxHeightCandles"() {
given: "A BirthdayCandles object"
def candles = new BirthdayCandles("$test")
when: "I call the max number height method"
def result = candles.getNumberOfMaxHeightCandles()
then: "I should get the frequency count of the max number in the integer array"
result == "$result"
where:
test | result
[1,1,1,3,3,3,3] | 4
[1,1,1,3,3,3,4] | 1
}
只需添加行即可添加測(cè)試變體。
添加回答
舉報(bào)