Context receiver mocking with mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myexample | |
import io.mockk.every | |
import io.mockk.mockk | |
import io.mockk.verify | |
import org.junit.Test | |
import kotlin.test.assertEquals | |
interface CallContext | |
class MyClass { | |
context(CallContext) | |
fun myMethod(a: Int): Int = a | |
} | |
class ContextMockTest { | |
private val myClassMock: MyClass = mockk() | |
@Test | |
fun mockContextWorks() { | |
every { | |
with(any<CallContext>()) { | |
myClassMock.myMethod(any()) | |
} | |
} returns 123 | |
val context = object : CallContext { } | |
with(context) { | |
assertEquals(123, myClassMock.myMethod(1)) | |
} | |
verify { | |
with(any<CallContext>()) { | |
myClassMock.myMethod(1) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment