2012-11-01 45 views
2

我無法理解如何使用void方法爲spock設置參數化測試。 這是一個鏈表我簡單的測試案例:使用void方法進行參數化測試

@Unroll 
def "should delete the element #key and set the list size to #listSize"(key, listSize) { 
    given: 
    list.insert(6) 
    list.insert(12) 
    list.insert(33) 

    expect: 
    def deletedKey = list.delete(key) 
    list.size() == listSize 

    where: 
    key || listSize 
    6 || 2 
    12 || 2 
    33 || 2 
    99 || 3 
} 

方法delete()是一種無效的方法,但如果我不明確地得到一個返回值,則測試失敗。

這實際上是工作:

expect: 
def deletedKey = list.delete(key) 
list.size() == listSize 

雖然這並不:

expect: 
list.delete(key) 
list.size() == listSize 

試驗報告抱怨空

Condition not satisfied: 

list.delete(key) 
| |  | 
| null 12 
[email protected] 

如何管理這種情況?我想在刪除方法被調用後測試刪除檢查列表狀態的結果。

感謝, 卡羅

回答

2

如果使用whenthen而不是expect是否行得通?

@Unroll 
def "should delete the element #key and set the list size to #listSize"(key, listSize) { 
    given: 
    list.insert(6) 
    list.insert(12) 
    list.insert(33) 

    when: 
    list.delete(key) 

    then: 
    list.size() == listSize 

    where: 
    key || listSize 
    6 || 2 
    12 || 2 
    33 || 2 
    99 || 3 
} 
+1

是的,它確實有效。謝謝,卡羅 –

+1

不用擔心:-)很高興幫助!玩的開心! –