2

我不明白如何使用 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()是一个 void 方法,但如果我没有明确获得返回值,则测试失败。

这实际上是有效的:

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

虽然这不是:

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

测试报告抱怨null

Condition not satisfied:

list.delete(key)
|    |      |
|    null   12
com.github.carlomicieli.dst.LinkedList@5c533a2

我该如何处理这种情况?我想在调用删除方法后测试删除检查列表状态的结果。

谢谢,卡罗

4

1 回答 1

2

如果你使用whenandthen而不是它会起作用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
}
于 2012-11-01T09:47:49.703 回答