Closed svenjacobs closed 4 years ago
Thanks for the report @svenjacobs. Are you able to provide a reproducer? My guess is that mockk doesn't play well with coroutines.
My guess is that mockk doesn't play well with coroutines.
Did something change between 2.0.12 and 2.0.13 regarding coroutines? The release doesn't mention any major change...
MockK and coroutines work fine with 2.0.12. Tests have not been changed.
I think one change is that, discovery is now done within a coroutine to support asynchronous discovery: https://github.com/spekframework/spek/blob/2.0.x/spek-runtime/src/commonMain/kotlin/org/spekframework/spek2/runtime/SpekRuntime.kt#L48. But it is turned off by default, a coroutine is started but it is effectively single threaded and using the same thread as before (in 2.0.12).
Can you give me a sample that fails?
Here's a simple test that fails. The problematic line is 19 (coEvery
):
class StringProvider {
suspend fun world() = "world"
}
class Example(
private val stringProvider: StringProvider,
) {
suspend fun hello(): String {
delay(250)
return stringProvider.world()
}
}
object ExampleTest : Spek(
{
val stringProvider by memoized {
mockk<StringProvider> {
coEvery { world() } returns "world 2"
}
}
val example by memoized {
Example(stringProvider)
}
describe("ExampleTest") {
it("should work ;)") {
runBlockingTest {
example.hello() shouldBeEqualTo "world 2"
}
coVerify {
stringProvider.world()
}
}
}
}
)
Interestingly the tests do not fail when run individually from IDE but fail when run with ./gradlew test
.
A few things to test out (I'll setup a project myself later, can't right now):
org.gradle.parallel=false
to gradle.properties
.coEvery { world() } returns "world 2"
to a beforeEachTest
.In both cases tests unfortunately still fail :(
Weird, hmm. I'll have to investigate some more. Can you wrap stringProvider.world()
in a runBlockingTest
?
coVerify {
runBlockingTest { stringProvider.world() }
}
The idea here is to call stringProvider.world()
using the same dispatcher.
Neither wrapping coVerify
nor coEvery
in runBlockingTest
did fix this.
Should be resolved in #925.
Any chance of a new release with this bugfix soon? 😃
Maybe in two weeks time, I’m really having trouble finding time for my personal projects at the moment. 😢
@svenjacobs Can you try this build out? https://bintray.com/spekframework/spek-dev/spek2/2.0.14-alpha.0.4%2B88d54d2 Hopefully it fixes the issue :crossed_fingers:
@svenjacobs Can you try this build out? https://bintray.com/spekframework/spek-dev/spek2/2.0.14-alpha.0.4%2B88d54d2 Hopefully it fixes the issue 🤞
Yes, tests work with this version 👍 I just compared it to 2.0.13 were tests fail.
Awesome, I'll start the release process sometime this week when I get the time. Thanks for checking!
@svenjacobs sorry for the delay, 2.0.14
should be out now.
Starting with Spek 2.0.13 suddenly many tests fail that have not been changed in any way. Just Spek was updated. The error messages are something like:
MockK version 1.10.0. This is the same MockK version that was used with Spek 2.0.12 where tests still work.