LiveData not captured when unit testing Either.fold()

112 Views Asked by At

I'm trying to add unit tests to a project that we've inherited from another company, but I can't manage to make them work since I'm not very experienced with testing and I never used the Arrow-kt library.

The function GetAEQueries.Params.forAEQueryRequest(queryRequest) returns an Either<Failure, List<AEQuery>>

They use the following architecture for Viewmodels:

    private var _queryList = MutableLiveData<Result<List<AEQuery>>>()
    val queryList: LiveData<Result<List<AEQuery>>> get() = _queryList    

    fun foo(queryRequest: AEQueryRequest, consultationType: ConsultationType) {
        _lastFilter.postValue(queryRequest)
        _queryList.postValue(Result.Loading())
        launch {
            getAEQueries(GetAEQueries.Params.forAEQueryRequest(queryRequest)).fold(
                {
                    // Failure
                    _queryList.postValue(Result.Error(it))
             
                },
                { queries ->
                    // Success
                    _queryList.postValue(Result.Success(queries))
                }
            )
        }
    }

And this is the test I'm trying to run

    @get:Rule
    val instantRule = InstantTaskExecutorRule()

    private val testDispatcher = TestCoroutineDispatcher()

    @Before
    fun setUp() {
        Dispatchers.setMain(testDispatcher)

        viewModel = ConsultationsListViewModel(
            testDispatcher,
            appNavigator,
            transactionsNavigator,
            dialogNavigator,
            getAEQueries,
            getAEServices,
            getAEDownload
        )
    }

    @After
    fun tearDown() {
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()
    }

    @Test
    fun `requestToGetAEQueries OK should update queryList with the result`() = runTest {
        // Given
        val queryRequest = // Mocked request
        val consultationType = // Mocked consultation type
        val expectedQueries = // Mocked return
        val expectedResult: Either<Failure, List<AEQuery>> = expectedQueries.right()

        coEvery { getAEQueries(GetAEQueries.Params.forAEQueryRequest(queryRequest)) } returns expectedResult

        // When
        viewModel.requestToGetAEQueries(queryRequest, consultationType)

        // Then
        viewModel.lastFilter.observeOnce {
            assert(it == queryRequest)
        }
        viewModel.queryList.observeOnce {
            if(it is Result.Loading) {
                assert(it == Result.Loading(null))
            } else if (it is Result.Success) {
                assert(it == Result.Success(expectedQueries))
            }
        }
    }

The problem that I've got here is that the test captures the lastFilter LiveData correctly, but doesn't capture anything inside the viewmodel's .fold() function so it never reaches the Result.Success condition.

What am I missing here? Thanks in advance

0

There are 0 best solutions below