https://kotlinlang.org logo
Title
r

Rak

01/05/2022, 5:19 PM
Hi, I am trying to write my first unit test for my viewmodel but it doesnt work
Here’s my test
@Test
fun firstTest() = runBlocking {

    // given
    coEvery { repo.getServerTime() } returns Result.success(aDate)
    coEvery { repo.getUser() } returns ApiResultWrapper.Success(aUserResponse)
    every { prefs.getPass(any()) } returns null

    // when
    val testSubject = HubViewModel(authService, repo, prefs).test(HubState(loading = true))
    testSubject.runOnCreate()
    testSubject.testIntent {
        loadUser()
    }

    // then
    testSubject.assert(HubState(loading = true)) {
        states(
            { copy(loading = false) }
        )
    }
}
It just runs indefinately, never completing
m

miqbaldc

01/06/2022, 3:38 AM
could you show us your
HubViewModel
snippet code?
r

Rak

01/06/2022, 8:13 AM
class HubViewModel(
    private val authService: AuthService,
    private val repo: MerlinRepository,
    private val prefs: Preferences,
) : BaseVM(), ContainerHost<HubState, NavigationEvent> {

    override val container: Container<HubState, NavigationEvent> =
        container(HubState(loading = true)) {
            loadUser()
        }

    fun loadUser() = intent {
        repeatOnSubscription {
            val serverTimeResult = repo.getServerTime()
            if (serverTimeResult.isFailure) {
                postSideEffect(GlobalNavigationEvent.NavigateToGenericError)
            }
            when (val response = repo.getUser()) {
a

appmattus

01/06/2022, 8:56 AM
could be an issue with
repeatOnSubscription
@Mikolaj Leszczynski?
r

Rak

01/06/2022, 8:59 AM
let me try and remove it….
yep, test no longer hangs
:thank-you: 1
m

Mikolaj Leszczynski

01/06/2022, 7:29 PM
Looks like
repeatOnSubscription
might never be subscribing in unit tests! I’ll file an issue.
:thank-you: 1