Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…o codeborne-dev
  • Loading branch information
nhaarman committed Mar 10, 2016
2 parents 5105493 + 9238c4c commit fc95068
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ import org.mockito.ArgumentCaptor

inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
inline fun <reified T : Any> capture(noinline consumer: (T) -> Unit): T {
var times = 0
return argThat { if (++times == 1) consumer.invoke(this); true }
}
12 changes: 11 additions & 1 deletion mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.*
class ArgumentCaptorTest {

@Test
fun captor() {
fun explicitCaptor() {
val date: Date = mock()
val time = argumentCaptor<Long>()

Expand All @@ -18,4 +18,14 @@ class ArgumentCaptorTest {
verify(date).time = capture(time)
expect(time.value).toBe(5L)
}

@Test
fun implicitCaptor() {
val date: Date = mock()
date.time = 5L

verify(date).time = capture {
expect(it).toBe(5L)
}
}
}

0 comments on commit fc95068

Please sign in to comment.