Skip to content

Commit 6f9e1a7

Browse files
Danny Preusslernhaarman
authored andcommitted
better inOrder for single arguments
1 parent d4b8fec commit 6f9e1a7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/Verification.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ fun inOrder(
199199
Mockito.inOrder(*mocks).evaluation()
200200
}
201201

202+
/**
203+
* Allows [InOrder] verification for a single mocked instance:
204+
*
205+
* mock.inOrder {
206+
* verify().foo()
207+
* }
208+
*
209+
*/
210+
inline fun <T> T.inOrder(block: InOrderOnType<T>.() -> Any) {
211+
block.invoke(InOrderOnType(this))
212+
}
213+
214+
class InOrderOnType<T>(private val t: T) : InOrder by inOrder(t as Any) {
215+
216+
fun verify(): T = verify(t)
217+
}
218+
202219
/**
203220
* Allows checking if given method was the only one invoked.
204221
*/

tests/src/test/kotlin/test/VerificationTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ class VerificationTest : TestBase() {
6464
}
6565
}
6666

67+
@Test
68+
fun testInOrderWithReceiver() {
69+
/* Given */
70+
val mock = mock<Methods>()
71+
72+
/* When */
73+
mock.string("")
74+
mock.int(0)
75+
76+
/* Then */
77+
mock.inOrder {
78+
verify().string(any())
79+
verify().int(any())
80+
verifyNoMoreInteractions()
81+
}
82+
}
83+
6784
@Test
6885
fun testClearInvocations() {
6986
val mock = mock<Methods>().apply {

0 commit comments

Comments
 (0)