junit 如何使用Mockk创建观察者

drkbr07n  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(227)

你好,我有一个使用Mockito的单元测试,我将它的大部分转换为使用Mockk,除了一部分,我想创建一个Android生命周期的观察者
Mockito版本的工作原理

@Mock
private lateinit var dataObserver: Observer<Result<List<Character>>>

不工作的模拟版本

private var dataObserver: Observer<Result<List<Character>>> = mockk(relaxed = true)

当我使用Mockk进行上述操作时,我的测试用例失败,并出现以下错误;当我使用Mockito版本时,测试用例通过
错误讯息

java.lang.AssertionError: Verification failed: call 1 of 1: Observer(#2).onChanged(eq(Success([Character(name=myName, img=image, occupation=[], status=status, nickname=nickName, appearance=[])])))) was not called

    at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
    at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
    at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
    at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalCoVerify(API.kt:143)
    at io.mockk.MockKKt.coVerify(MockK.kt:175)
    at io.mockk.MockKKt.coVerify$default(MockK.kt:172)
    at com.example.breakingbad.MainActivityViewModelTest$fetchCharacters$1.invokeSuspend(MainActivityViewModelTest.kt:76)

完整测试用例

package com.example.breakingbad

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import com.example.breakingbad.data.DataRepository
import com.example.breakingbad.model.Character
import com.example.breakingbad.viewModel.MainActivityViewModel
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner

//@RunWith(MockitoJUnitRunner::class)

@ExperimentalCoroutinesApi
class MainActivityViewModelTest {

    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    private val testDispatcher = TestCoroutineDispatcher()
    private val testCoroutineScope = TestCoroutineScope(testDispatcher)

    private val dataRepository: DataRepository = mockk(relaxed = true)

    private val mainActivityViewModel = MainActivityViewModel(dataRepository)

//    @Mock
//    private lateinit var dataObserver: Observer<Result<List<Character>>>

    private var dataObserver: Observer<Result<List<Character>>> = mockk(relaxed = true)

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

    @After
    fun cleanup() {
        Dispatchers.resetMain()
        testCoroutineScope.cleanupTestCoroutines()
    }

    @Test
    fun fetchCharacters() {
        testCoroutineScope.launch {

            coEvery { dataRepository.getCharacters() } returns Result.success(arrayListOf(Character(
                    name = "myName",
                    img = "image",
                    occupation = arrayListOf(),
                    status = "status",
                    nickname = "nickName",
                    appearance = arrayListOf()
            )))

            mainActivityViewModel.fetchCharacters()
            coVerify { dataRepository.getCharacters() }

            coVerify { dataObserver.onChanged(
                    Result.success(listOf(Character (
                            name = "myName",
                            img = "image",
                            occupation = arrayListOf(),
                            status = "status",
                            nickname = "nickName",
                            appearance = arrayListOf()
                    )))
            ) }
            mainActivityViewModel.charactersLiveData.removeObserver(dataObserver)
        }
    }
}

如何使用Mockk使测试用例通过?2我做错了什么?

编辑

检视模型

class MainActivityViewModel @Inject constructor(
        private val dataRepository: DataRepository
): ViewModel() {
    private val _charactersLiveData = MutableLiveData<Result<ArrayList<Character>>>()
    val charactersLiveData: LiveData<Result<ArrayList<Character>>> = _charactersLiveData

    fun fetchCharacters() {
        viewModelScope.launch(Dispatchers.IO) {
            _charactersLiveData.postValue(dataRepository.getCharacters())
        }
    }
}
vwkv1x7d

vwkv1x7d1#

一个测试liveData的表单正在捕获观察者通过的数据。
PD:在你的情况下,他可能忘记了在livedata中添加observerForever。
总之我留下一个例子:

private val dataObserver: Observer<Result<List<Character>>> = mockk()

private val argumentCaptor: CapturingSlot<Result<List<Character>> = slot()

@Test
fun fetchCharacters() = runBlockingTest {

    //Move to @Before    
    mainActivityViewModel.charactersLiveData.observerForever(dataObserver)

    coEvery { dataRepository.getCharacters() } returns Result.success(arrayListOf(Character(
        name = "myName",
        img = "image",
        occupation = arrayListOf(),
        status = "status",
        nickname = "nickName",
        appearance = arrayListOf()
    )))

    mainActivityViewModel.fetchCharacters()

    coVerify { dataRepository.getCharacters() }
    verify { dataObserver.onChanged(capture(argumentCaptor)) }

    with(argumentCaptor){

        //Here can be assert data, example:
        assert(this is Result.Success)
        assertEquals(YourData, this.data) //validation according to your data structure
    }

    //Move to @After
    mainActivityViewModel.charactersLiveData.removeObserver(dataObserver)
}

相关问题