使用void方法的参数化测试

时间:2012-11-01 09:39:43

标签: groovy spock

我无法理解如何使用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

测试报告抱怨空

Condition not satisfied:

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

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

谢谢, 卡罗

1 个答案:

答案 0 :(得分: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
}
相关问题