You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When i created my own function mocking layer on top of Mockative 1.4.1 i created a simple way to block the return of a suspend function so that you can check a condition that only exists while the suspend function has not finished. For example, let's say you had a case like this:
and i want to test that isWorking gets set to true. I need to block the execution of mockedSuspendFunction and then resume it.
I can certainly do that manually by setting up the expectations all over again like
val deferred = CompletableDeferred<Unit>()
coEvery { mockedSuspendFunction() }
.invokes { _ ->
deferred.await()
// repeat the normal set up all over again
}
functionToTest()
assertTrue(isWorking)
deferred.complete(Unit)
assertFalse(isWorking)
That is a lot of ceremony and if the normal set up for the mock is complex is very verbose.
What i had done in my my Mockative wrapper for functions was to allow you to write that like this:
How could this be done with Mockative. I propose these additions to Mockable:
private var deferred: CompletableDeferred<Unit>? = null
/**
* Provides a way to have the return from the function to be delayed
* to verify status while the suspend call would be in progress
*/
internal fun pause() { deferred = CompletableDeferred() }
/**
* Resume any blocked calls to the function
*/
internal fun resume() { deferred?.complete(Unit) }
then in Mockable.suspend on all return and throws add a .also { deferred?.await() }
When i created my own function mocking layer on top of Mockative 1.4.1 i created a simple way to block the return of a suspend function so that you can check a condition that only exists while the suspend function has not finished. For example, let's say you had a case like this:
and i want to test that isWorking gets set to true. I need to block the execution of mockedSuspendFunction and then resume it.
I can certainly do that manually by setting up the expectations all over again like
That is a lot of ceremony and if the normal set up for the mock is complex is very verbose.
What i had done in my my Mockative wrapper for functions was to allow you to write that like this:
How could this be done with Mockative. I propose these additions to Mockable:
then in Mockable.suspend on all return and throws add a
.also { deferred?.await() }
Then the definition of whilePaused is:
The text was updated successfully, but these errors were encountered: