Sevastyan Savanyuk
Sevastyan Savanyuk

Reputation: 6315

What's the difference between `verifySequence` and `verifyOrder` in MockK?

In the guide for MockK library, the example is not clearing this for me. Here follows the example from the documentation:

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
}

Upvotes: 24

Views: 6984

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 82017

The method verifySequence checks that only the specified calls happened and this must be in the same order. Method verifyOrder on the other hand also works if you leave out some calls as you already did in the example (obj.sum(1, 3)). The following will fail because you cannot leave out a call with verifySequence:

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

Upvotes: 26

Related Questions