Hey guys. A few weeks ago, I introduced Micro-Mock...
# multiplatform
s
Hey guys. A few weeks ago, I introduced Micro-Mock, a mocking processor & runtime for Kotlin Multiplatform that allows to mock interfaces with the same syntax & semantics than MockK or Mockito-Kotlin. Today I'm announcing that it has been renamed MocKMP and promoted to stable. Here is the announcement article: https://medium.com/kodein-koders/mockmp-a-mocking-processor-for-kotlin-multiplatform-51957c484fe5 The library itself lives here: https://github.com/Kodein-Framework/MocKMP We hope you'll find it usefull.
👍 6
🥳 5
K 14
🙌 5
p
Technically, we should be able to generate implementations of abstract class or open class in Kotlin,
Have you evalulated if a mechanism like https://kotlinlang.org/docs/all-open-plugin.html could help here and merry it with mockmp?
s
Well, that would only work on classes of the current module. MocKMP allows you to mock any interface, whether from a library or your module sources.
a
@salomonbrys Is there a sample project I can use to figure out how to use it. I tried it with Kotlin 1.6.0 and the setup with the plugin, but, it doesn't seem to be generating the mocks.
Answering my own question: I needed to run testDebugUnitTest to generate the class. Then added the generated srcdir to commonTest.
👍 1
Ran into another problem though with trying to run the test on iOS: : Could not determine the dependencies of task 'sharedcompileTestKotlinIosX64'.
Task with path 'kspTestKotlinAndroid' not found in project ':shared'.
s
@anar can you provide a reproducer ?
a
Should I open an issue and add it on the project?
Issue opened. Thanks for responding.
s
I'll have a look asap 🙂
👍 1
a
Hello,
How can i test interfaces with suspend functions ?
s
Hi @Ahmet Özcan, Mocking interfaces with suspend functions should be supported. If you find a use-case where it is not the case, please open an issue with a reproducer.
a
this code block not get compiled
Copy code
mocker.every{
    remoteDataSource.checkUserName(isAny())
} returns DataState.Success(CheckUserNameResponse(true))
because checkUserName is a suspend function
im missing something for sure but what 😄
when i call as below its ok. But the runBlocking method is coming from my framework with exptect/actual mech.
Copy code
mocker.every{
    runBlocking {
        remoteDataSource.checkUserName(isAny())
    }
} returns DataState.Success(CheckUserNameResponse(true))
is there any other way to do that ?
Full code is
Copy code
runTest {
    mocker.every{
        runBlocking {
            remoteDataSource.checkUserName(isAny())
        }
    } returns DataState.Success(CheckUserNameResponse(true))
    state.test {
        SUT.sendIntent(RegisterWithPhoneViewStore.Intent.SetPhoneNumber(validPhoneNumber))
        var item = expectMostRecentItem()
        assertEquals(item.phoneNumber, validPhoneNumber)
        SUT.sendIntent(RegisterWithPhoneViewStore.Intent.ValidatePhoneNumber)
        item = awaitItem()
        assertTrue(item.inProgress)
        item = awaitItem()
        assertTrue(!item.inProgress)
    }

    navigation.test {
        SUT.sendIntent(RegisterWithPhoneViewStore.Intent.ValidatePhoneNumber)
        val item = awaitItem()
        println(item)
        assertTrue(item is Destination.LoginScreen)
    }

    assertTrue(onboardingManager.phoneNumber.isEmpty())
    assertEquals(OnboardingType.Empty, onboardingManager.onboardingType)
}
s
Investigating...
a
🙏
s
@Ahmet Özcan Version
1.2.0
properly supports suspend functions. The documentation has been updated to reflect that : https://github.com/Kodein-Framework/MocKMP#defining-suspending-behaviour
a
Thank you very much Salomon. I ll be more then happy to use this great library K
👍 1