Skip to content

Commit

Permalink
refactor: changed asserts in tests with LiveData objects
Browse files Browse the repository at this point in the history
  • Loading branch information
joragua committed Dec 12, 2024
1 parent 02c5ef4 commit d221687
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.owncloud.android.data.sharing.shares.repository

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.owncloud.android.data.sharing.shares.datasources.LocalShareDataSource
Expand All @@ -31,14 +32,20 @@ import com.owncloud.android.testutil.OC_PRIVATE_SHARE
import com.owncloud.android.testutil.OC_PUBLIC_SHARE
import com.owncloud.android.testutil.OC_SHARE
import com.owncloud.android.testutil.OC_SHAREE
import com.owncloud.android.testutil.livedata.getLastEmittedValue
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import junit.framework.TestCase.assertEquals
import org.junit.Rule
import org.junit.Test

class OCShareRepositoryTest {

@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()

private val localShareDataSource = mockk<LocalShareDataSource>(relaxUnitFun = true)
private val remoteShareDataSource = mockk<RemoteShareDataSource>(relaxUnitFun = true)
private val ocShareRepository = OCShareRepository(localShareDataSource, remoteShareDataSource)
Expand Down Expand Up @@ -187,7 +194,7 @@ class OCShareRepositoryTest {

@Test
fun `getSharesAsLiveData returns a LiveData with a list of OCShares`() {
val sharesLiveDataList: LiveData<List<OCShare>> = MutableLiveData(listOfShares)
val sharesLiveDataList: LiveData<List<OCShare>> = MutableLiveData(listOf(OC_SHARE))

every {
localShareDataSource.getSharesAsLiveData(
Expand All @@ -197,8 +204,8 @@ class OCShareRepositoryTest {
)
} returns sharesLiveDataList

val sharesResult = ocShareRepository.getSharesAsLiveData(filePath, OC_ACCOUNT_NAME)
assertEquals(sharesLiveDataList, sharesResult)
val sharesResult = ocShareRepository.getSharesAsLiveData(filePath, OC_ACCOUNT_NAME).getLastEmittedValue()
assertEquals(sharesLiveDataList.value, sharesResult)

verify(exactly = 1) {
localShareDataSource.getSharesAsLiveData(
Expand All @@ -211,14 +218,14 @@ class OCShareRepositoryTest {

@Test
fun `getShareAsLiveData returns a LiveData with an OCShare`() {
val shareLiveData: LiveData<OCShare> = MutableLiveData(OC_SHARE)
val shareAsLiveData: LiveData<OCShare> = MutableLiveData(OC_SHARE)

every {
localShareDataSource.getShareAsLiveData(OC_SHARE.remoteId)
} returns shareLiveData
} returns shareAsLiveData

val shareResult = ocShareRepository.getShareAsLiveData(OC_SHARE.remoteId)
assertEquals(shareLiveData, shareResult)
val shareResult = ocShareRepository.getShareAsLiveData(OC_SHARE.remoteId).getLastEmittedValue()
assertEquals(shareAsLiveData.value, shareResult)

verify(exactly = 1) {
localShareDataSource.getShareAsLiveData(OC_SHARE.remoteId)
Expand Down

0 comments on commit d221687

Please sign in to comment.