I have a KMP library that I’m trying to write comm...
# multiplatform
e
I have a KMP library that I’m trying to write common tests for. My common code uses an expect/actual object to provide a platform specific dependency. Is there a way to provide a stub implementation for the commonTest module? Specifically, Android/iOS provide their own main and background Dispatchers. When running the commonTests I get the error:
Exception in thread "Test worker @coroutine#1" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
However, kotlinx-coroutines-test is only for the JVM, so I’m not sure what to do here.
b
As a general rule I tend to avoid expect/actual and use interfaces+DI instead, if it's not too much work you might be able to provide any Dispatcher implementation you want by just passing it to the object that uses it
r
+1 to interfaces for this sort of use-case. There’s no way to provide a different actual declaration for tests than for main sources.
k
Thirded