0

Im trying to make an integration test in my viewmodel, which a livedata has multiple value in it. At the beginning of a function, this livedata will have a value of Loading, and after the network call is finished, it will have a value of success or failure. But on my unit test, even if i delay it for 100 second, the value is still Loading and not replaced by the network call. Here is my livedata and function :

private val _loginInfo: MutableLiveData<NetworkResponse<LoginResponse>> = MutableLiveData()
    val loginInfo: LiveData<NetworkResponse<LoginResponse>> get() = _loginInfo

    fun signInWithEmailAndPassword(email: String, password: String) {
        _loginInfo.value = NetworkResponse.Loading
        viewModelScope.launch(Dispatchers.IO) {
            _loginInfo.postValue(
                repository.signInWithEmailAndPassword(
                    email = email,
                    password = password
                )
            )
        }
    }

and here is my unit test on the live data :

@Test
    fun `Failed login because wrong Email or Password`() = runTest {
        val expectedResult = NetworkResponse.GenericException(code = 401, cause = "Unauthorized")
        loginViewModel.signInWithEmailAndPassword(
            email = "myEmail@gmail.com",
            password = "myPassword"
        )
        val observer = Observer<NetworkResponse<LoginResponse>> {}
        loginViewModel.loginInfo.observeForever(observer)
        Assert.assertEquals(NetworkResponse.Loading, loginViewModel.loginInfo.value)
        delay(100000L)
        Assert.assertEquals(expectedResult, loginViewModel.loginInfo.value)
        loginViewModel.loginInfo.removeObserver(observer)
    }

The function on repository works fine, i already tested it on repository integration test. But the value of my livedata won't change. What is the problem on here? I also already setup the rule :

 @get:Rule
    val rule = InstantTaskExecutorRule()
lelestacia
  • 201
  • 2
  • 9

0 Answers0