MockK中的`verifySequence`和`verifyOrder`有什么区别?

时间:2018-10-31 08:52:22

标签: unit-testing kotlin mockk

在MockK库指南中,该示例并未为我清除。以下是文档中的示例:

class MockedClass {
    fun sum(a: Int, b: Int) = a + b
}

val obj = mockk<MockedClass>()
val slot = slot<Int>()

every {
    obj.sum(any(), capture(slot))
} answers {
    1 + firstArg<Int>() + slot.captured
}

obj.sum(1, 2) // returns 4
obj.sum(1, 3) // returns 5
obj.sum(2, 2) // returns 5

verifyAll {
    obj.sum(1, 3)
    obj.sum(1, 2)
    obj.sum(2, 2)
}

verifySequence {
    obj.sum(1, 2)
    obj.sum(1, 3)
    obj.sum(2, 2)
}

verifyOrder {
    obj.sum(1, 2)
    obj.sum(2, 2)
}

val obj2 = mockk<MockedClass>()
val obj3 = mockk<MockedClass>()
verify {
    listOf(obj2, obj3) wasNot Called
}

1 个答案:

答案 0 :(得分:1)

方法verifySequence检查是否仅发生了指定的调用,并且调用顺序必须相同。另一方面,如果您像在示例(verifyOrder中那样遗漏了一些调用,则方法obj.sum(1, 3)也可以使用。以下操作将失败,因为您不能留下与verifySequence的通话:

verifySequence {
    obj.sum(1, 2)
    obj.sum(2, 2)
}