Android 使用Spek和suspend方法
我试图在我的Android kotlin项目中使用Spek框架和Nharman mockito kotlin创建一个单元测试。问题是,当存在嵌套的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) 在任何描述之前 这是我的小组
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)
);