Mikolaj
07/19/2023, 11:04 AMflow
so I can bypass one line of code in my class. Basically I need to bypass the dropWhile
method so it passes the values in flow to the next method. I tried to create mock like that:
@Test
fun `test mock flow`() = runTest {
val flow: Flow<Int> = mockk(relaxed = true) {
coEvery { dropWhile(any()) } returns this
}
}
But then i get error Failed matching mocking signature for left matchers: [any()]
. Is it possible to create a mock that will bypass the dropWhile
method?
Thanks!Jakub Gwóźdź
07/19/2023, 11:29 AMJakub Gwóźdź
07/19/2023, 11:30 AMevery {…
instead of coEvery
should be enough, I thinkJakub Gwóźdź
07/19/2023, 11:46 AM@Test
fun `test mock flow`(): Unit = runBlocking {
val flow: Flow<Int> = flowOf(1, 2, 1, 3, 1, 4)
mockkStatic(Flow<Int>::dropWhile)
every { flow.dropWhile(any()) } answers { println("mock hit!"); flow }
flow.dropWhile { it < 3 }.collect { println(it) }
}
(also, thanks for the puzzle, I clearly needed a distraction 🙂 )Mikolaj
07/19/2023, 11:56 AMKFunction
using mockkStatic 😕 but that is entirely different problem and Your solution for mocking the flow
works! 😄Jakub Gwóźdź
07/19/2023, 12:37 PMmockkStatic("kotlinx.coroutines.flow.FlowKt")
should workMikolaj
07/19/2023, 12:54 PM