Create a helper for testing with suspending functions that need to hang for the tests (#249)

This commit is contained in:
David Perez 2023-11-15 15:57:59 -06:00 committed by Álison Fernandes
parent fd10de9456
commit 5c38eb743f
2 changed files with 34 additions and 12 deletions

View file

@ -14,21 +14,22 @@ import com.x8bit.bitwarden.data.platform.util.asFailure
import com.x8bit.bitwarden.data.platform.util.asSuccess
import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockSyncResponse
import com.x8bit.bitwarden.data.vault.datasource.network.service.SyncService
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult
import com.x8bit.bitwarden.data.vault.datasource.sdk.VaultSdkSource
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSdkCipher
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSdkFolder
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockFolderView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSdkCipher
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockSdkFolder
import com.x8bit.bitwarden.data.vault.repository.model.VaultData
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
import com.x8bit.bitwarden.testingtools.hangs
import com.x8bit.bitwarden.testingtools.just
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
@ -346,10 +347,7 @@ class VaultRepositoryTest {
organizationKeys = mapOf(),
),
)
} coAnswers {
delay(Long.MAX_VALUE)
Result.success(InitializeCryptoResult.Success)
}
} just hangs()
val scope = CoroutineScope(Dispatchers.Unconfined)
scope.launch {
@ -393,10 +391,7 @@ class VaultRepositoryTest {
organizationKeys = mapOf(),
),
)
} coAnswers {
delay(Long.MAX_VALUE)
Result.success(InitializeCryptoResult.Success)
}
} just hangs()
val scope = CoroutineScope(Dispatchers.Unconfined)
scope.launch {

View file

@ -0,0 +1,27 @@
package com.x8bit.bitwarden.testingtools
import io.mockk.MockKAdditionalAnswerScope
import io.mockk.MockKStubScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
/**
* A helper alias for [Hangs] to be consistent with the mockk DSL.
*
* Example: coEvery { service.suspend() } just hangs()
*/
typealias hangs<T> = Hangs<T>
/**
* A class that will hang indefinitely when invoked.
*/
class Hangs<T> {
suspend operator fun invoke(): T = MutableSharedFlow<T>().first()
}
/**
* A coAnswers placeholder for suspending functions that should never return.
*/
suspend infix fun <T, B> MockKStubScope<T, B>.just(
hangs: Hangs<T>,
): MockKAdditionalAnswerScope<T, B> = coAnswers { hangs() }