-
Notifications
You must be signed in to change notification settings - Fork 197
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
Coroutine/suspend function mocking support #205
Comments
Could you post a more elaborate example? |
interface AccountRepository {
suspend fun getAccountId(deviceNo: String): Int
}
class SUT(val repo: AccountRepository) {
fun something() {
launch(CommonPool) { println(repo.getAccountId("0000")) }
}
}
// Test function
val repoMock:AccountRepo = mock { onGeneric { runBlocking { getAccountId(any()) } } doReturn 3300 }
val sut = SUT(repoMock)
sut.something()
// Do some assertions on mock |
If the mock and onGeneric lambdas were either suspend functions or inline, then I wouldn't need to put runBlocking inside the onGeneric for every function, I could just surround the whole mock {} with runBlocking, it would be much cleaner. |
Isn't this gonna be sort of hard to implement if you want to have the library compatible with pre 1.1 kotlin? Maybe soon Gradle magic? |
Or maybe it's enough to just make them inline. |
It might be enough to make them into extension functions that are inline,
I'm just not sure about nested dsl working that way...
בתאריך 2 באוק׳ 2017 7:55 PM, "540Grunkspin" <notifications@github.com> כתב:
… Or maybe it's enough to just make them inline.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#205 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAxq9K9dyJM2_m5AXsAegppoxTWSGM6gks5soRWZgaJpZM4PnKkt>
.
|
I created a pull request for a suggested fix. Seems like all the previous tests are passing but still waiting for the CI to run. Not sure if this is exactly what you wanted. There is an example test in there as well. |
Thanks, I'm on vacation for a week, but when I get back I'll try it out!
I'm sure it'll help lots of people, if coroutines pick up as much as Kotlin
did...
בתאריך 3 באוק׳ 2017 6:46 PM, "540Grunkspin" <notifications@github.com> כתב:
… I created a pull request for a suggested fix. Seems like all the previous
tests are passing but still waiting for the CI to run.
Not sure if this is exactly what you wanted. There is an example test in
there as well.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#205 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAxq9DnzOWdWO4WH9KuZBtIdAVPun-_vks5solbQgaJpZM4PnKkt>
.
|
Any news on this? It's the same with
Creates quite some noise whenever there is a suspending function. |
I tried to switch to inline but that didn't really fix the problem, not sure if I'm smart or knowledgeable enough to fix it. Feel free to pick up where I left off. See my linked pull request. |
I don't really use coroutines much myself. I can suggest this solution: fun <T : Any, R> KStubbing<T>.onBlocking(
m: suspend T.() -> R
): OngoingStubbing<R> {
return runBlocking { Mockito.`when`(mock.m()) }
} .. but I don't know if it covers all scenarios. The following test passes:
|
The above solution works. |
what if I want to control when the suspending function returns? is this possible? |
@nhaarman |
Pointing to this version I was able to use directly the onBlocking function: I tried using latest version: |
The solution is based on nhaarman's comment in mockito/mockito-kotlin#205
I cannot find this method signature in your library @nhaarman , is this an assertion/verify mocking? EDITED: |
|
Nice thanks a lot @nhaarman |
While this might work fine for interface's suspend function it's not working for me in mocking below suspend function type someFunction: suspend () -> Boolean It throws on { invoke() }.doReturn { true } it also throws onGeneric { invoke() }.doReturn { true } then it works fine. In suspend case I am not sure what might be the alternative for that. Anyone has any clue how I can approach this? |
If you use non-inline lambdas that are not declared as suspend, the runBlocking { } around the test function doesn't help, rather we need to
mock { on { runBlocking { suspendFun() } } }
for each mock...The text was updated successfully, but these errors were encountered: