Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 971
Sevastyan Savanyuk Avatar asked Oct 31 '18 08:10

Sevastyan Savanyuk


1 Answers

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)
}
like image 108
s1m0nw1 Avatar answered Nov 07 '22 09:11

s1m0nw1