Adam S
12/08/2022, 9:26 AMevery {}
and coEvery {}
. It makes sense, because Kotlin can’t allow suspend funs to be overloaded, so the names have to be different. But it’s still awkward. If I refactor my code to be suspending, I have to rename lots of mocking functions. And if I don’t use coroutines, the coEvery
, coVerify
, coAssert
auto completes aren’t useful, and clutter my auto-complete.
But… what if MockK provided a wrapper function for tests?
@Test
fun myTest() = coMock {
// non suspending function - will use 'every {}'
every { nonSuspendingFunction() } returns 123
// a suspending function - can also use 'every {}'
every { suspendingFunction() } returns 999
}
suspend fun coMock(context: CoMockKContext.() -> Unit)
would provide a coroutine context, and the receiver, CoMockKContext
, would provide suspend fun
versions of the standard MockK DSL.
Pros:
• no more need for all the MockK functions to be duplicated according to suspend/regular functions.
• as a MockK user, if I refactor my code to be suspend/non-suspend then I don’t need to rename the mocking functions
• if I have a non-suspending codebase my auto-complete isn’t cluttered with suspend MockK DSL variants - coroutines become ‘opt-in’