Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Spek和suspend方法_Android_Kotlin_Spek_Mockito Kotlin - Fatal编程技术网

Android 使用Spek和suspend方法

Android 使用Spek和suspend方法,android,kotlin,spek,mockito-kotlin,Android,Kotlin,Spek,Mockito Kotlin,我试图在我的Android kotlin项目中使用Spek框架和Nharman mockito kotlin创建一个单元测试。问题是,当存在嵌套的suspend方法时,我不知道如何模拟响应 我定义: val testCoroutineDispatcher = TestCoroutineDispatcher() val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher) 在任何描述之前 这是我的小组

我试图在我的Android kotlin项目中使用Spek框架和Nharman mockito kotlin创建一个单元测试。问题是,当存在嵌套的suspend方法时,我不知道如何模拟响应

我定义:

    val testCoroutineDispatcher = TestCoroutineDispatcher()
    val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
在任何描述之前

这是我的小组

    describe("Test view model") {
        val contentRepository by memoized(CachingMode.SCOPE) { mock<ContentRepository>() }
        val contentViewModel by memoized(CachingMode.SCOPE) {
            ContentViewModel(contentRepository)
        }
        describe("When something happens") {
            beforeGroup {
                testCoroutineScope.runBlockingTest {
                    whenever(contentRepository.fetchAllContents(0, 10))
                        .thenReturn(Result.success(content))//This is suspend
                    contentViewModel.setContentPage(0)
                }
            }
            it("should fetch all content from repository with page 0") {
                verifyBlocking(contentRepository) {
                    fetchAllClassContents(0, 10)
                }
            }
        }
    }
})

看起来模拟、方法执行和断言在不同的范围内运行

我找不到任何指南可以帮助我使用协同程序创建测试 提前谢谢

    describe("Test view model") {
        val contentRepository by memoized(CachingMode.SCOPE) { mock<ContentRepository>() }
        val contentViewModel by memoized(CachingMode.SCOPE) {
            ContentViewModel(contentRepository)
        }
        describe("When something happens") {
            beforeGroup {
                testCoroutineScope.runBlockingTest {
                    whenever(contentRepository.fetchAllContents(0, 10))
                        .thenReturn(Result.success(content))//This is suspend
                    contentViewModel.setContentPage(0)
                }
            }
            it("should fetch all content from repository with page 0") {
                verifyBlocking(contentRepository) {
                    fetchAllClassContents(0, 10)
                }
            }
        }
    }
})

Argument(s) are different! Wanted:
classContentRepository.fetchAllClassContents(
    0,
    10,
    Continuation at viewmodel.ContentViewModelSpek$1$3$1$4$1.invokeSuspend(ContentViewModelSpek.kt:92)
);
-> at repository.ContentRepository.fetchAllClassContents(ContentRepository.kt:23)
Actual invocation has different arguments:
contentRepository.fetchAllContents(
    0,
    10,
    Continuation at viewmodel.ContentViewModel$setContentPage$1.invokeSuspend(ContentViewModel.kt:26)
);