I am using MVVM to architecutre my android app, my repository has a method which query data from Room Database and returns a LiveData, the signure of my method is:
fun getFolder(id: Long): LiveData<Folder?>
I want to write a unit test for this method with the following code:
import androidx.lifecycle.MutableLiveData
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import my.package.Folder
import my.package.FolderRepository
import java.util.*
class FolderRepositoryTest: FunSpec({
val repository = mockk<FolderRepository>()
val folder = Folder(
// folder field init code
)
val folderLiveData = MutableLiveData(folder)
test("FolderRepository getFolder works as expected") {
val id = folder.id.toLong()
every { repository.getFolder(any()) } returns folderLiveData
repository.getFolder(id)
verify {
repository.getFolder(id)
} shouldBe folderLiveData
}
})
But the test failed wit the following failure message.
io.kotest.assertions.AssertionFailedError: expected:androidx.lifecycle.MutableLiveData@1afc7182 but was:<kotlin.Unit>
expected:<androidx.lifecycle.MutableLiveData@1afc7182> but was:<kotlin.Unit>
Expected :androidx.lifecycle.MutableLiveData@1afc7182
Actual :kotlin.Unit
Can anybody help me point out where I am wrong and how to write unit test cases with kotest library and Mockk library.