New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: exception while calling mocked suspend function #288
Comments
Seems it is a bit tricky. And I think it was working before. Got following message:
for this code that mimics what mockk does:
|
I found some workaround - check if the actual last argument is a continuation, not just method types. Sad that more and more workarounds are needed, but I think anyway it has it's value. |
I was injecting a suspending function through the constructor. A workaround is to declare a regular interface with an So for example my class under test had a function declared in the constructor: private val api: suspend (Key) -> Value As a workaround instead of mocking the suspending function private val api : suspend (Int) -> String = mockk() I created an interface: private interface TestApi<Key,Value> {
suspend operator fun invoke(key: Key): Value
} and I'm passing a method reference: private val api: TestApi<Int,String> = mockk()
val repo = Repository(
api = api::invoke
) Now at the mocking side the usage stays the same: coEvery { api(key) } returns freshValue |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
Please put an |
I think I am getting the same thing @Test
fun `should listen to publish of a String`() {
val localEventScope = LocalEventScope()
val onReceiveMock = mockk<suspend (String) -> Unit>()
coEvery { onReceiveMock.invoke(any<String>()) } returns Unit
localEventScope.listen<String>(this, Dispatchers.Default, onReceiveMock)
localEventScope.publish("test string")
coVerify { onReceiveMock.invoke(any()) }
}
|
Any news about this issue? |
I have also experienced this, is there a chance to fix it? |
@gladed Could you please explain what do you exactly mean by
? |
@kdotdi See my post which shows an example for that. |
interface Suspender {
suspend fun foo(): Int
}
@Test
fun suspendMockTest() {
val happyMock = mockk<Suspender>()
coEvery { happyMock.foo() } returns 5
runBlocking { assertEquals(5, happyMock.foo()) } // Pass
val sadMock = mockk<suspend () -> Int>()
coEvery { sadMock() } returns 5
runBlocking { assertEquals(5, sadMock()) } // throws MockKException
} |
I'm getting the same error for the following code:
This is the failing test:
|
Cannot really use this workaround @gladed is providing. I am mocking object that is injected via DI and that object's suspend method has to return value. I cannot wrap anything within interfaces as it would mess up the production code. |
@p1415604x doesn't my workaround work? |
In version 1.10.0 i am still getting the same error Exception in thread "main @coroutine#5" io.mockk.MockKException: no answer found for: I am using Room db and calling its dao methods which are suspended Example interface TestDao{ class Test{ } @test |
Any news on this bug ? |
The same error. CoEvery works only if you transfer it to setup. |
Any update on this issue? |
I'm having the same issue, any update? |
Also having this same issue |
Any update? facing same issue |
Any update? same here |
3rd birthday coming up, let's throw a party. Who brings cake? |
This is not how you should ask for an update on a github issue. Take notes, kids. |
Instead of taking notes I suggest to submit a PR instead of complaing 😊 |
I don't know if it'll be of help, but I get this error when I start using mocked or spied values instead of constants in a Class being tested is Working: val testCode = "testCode"
val testLanguage = "testLanguage"
val parameters : Parameters = mockk()
every { parameters.code } returns testCode
every { parameters.language } returns testLanguage
// Note that it doesn't use `parameters` but constants directly
coEvery {
storeRepository.getStores(
testCode,
testLanguage
)
} returns testStoreList This is also working: val parameters : Parameters = Parameters.TestParameters
coEvery {
storeRepository.getStores(
parameters.code,
parameters.language
)
} returns testStoreList This one ends up with error in this issue: val parameters : Parameters = mockk() // or spyk(Parameters.TestParameters)
// No need for these if using spyk
every { parameters.code } returns testCode
every { parameters.language } returns testLanguage
coEvery {
storeRepository.getStores(
parameters.code,
parameters.language
)
} returns testStoreList I don't know the internals of the lib, but looks like fields of mocked or spied properties somehow are not matching inside UpdateIt seems something similar happens for This verification works: val parameters : Parameters = mockk()
every { parameters.code } returns testCode
every { parameters.language } returns testLanguage
coVerify(exactly = 3) { parameters.code }
coVerify(exactly = 3) { parameters.language }
coVerify(exactly = 1) {
storeService.getStoreListAsync(
testCode,
testLanguage
)
} But when I change it to this: val parameters : Parameters = mockk()
every { parameters.code } returns testCode
every { parameters.language } returns testLanguage
coVerify(exactly = 3) { parameters.code }
coVerify(exactly = 3) { parameters.language }
coVerify(exactly = 1) {
storeService.getStoreListAsync(
parameters.code,
parameters.language
)
} both |
Hello, no contribution here - just following with the same issue.
|
I have similar code above utilizing an interface that I mockked... I am curious what is the difference with yours that it is passing but mine is not...? |
I came across this issue myself today. I used this as a workaround:
Instead of using the framework to In instances where an actual suspend function is passed through dependency injection for example, you already have the ability to pass a real function rather than a mock. This can be any function that does the verify for you. This particular workaround may not suitable in all the cases mentioned already. It could help someone who is doing something similar to myself, however, so I thought I would share. |
In Version 1.9.3, calling a mocked suspending function throws "no answer found" even if an answer was provided.
suspend
.Here's the exception:
The text was updated successfully, but these errors were encountered: