问题描述
我试图在我的Android Kotlin项目中使用Spek框架和nhaarman嘲笑kotlin创建一个单元测试。问题是当有嵌套的suspend方法时,我不知道如何模拟响应。这就是我正在尝试的方法
我定义了:
val testCoroutinedispatcher = TestCoroutinedispatcher()
val testCoroutinescope = TestCoroutinescope(testCoroutinedispatcher)
以及任何描述
之前 beforeGroup {
dispatchers.setMain(testCoroutinedispatcher) //not sure if this is working properly
}
afterGroup {
dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
testCoroutinescope.cleanupTestCoroutines()
}
这是我的小组
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,Continuation at viewmodel.Contentviewmodel$setContentPage$1.invokeSuspend(Contentviewmodel.kt:26)
);
模拟,方法执行和断言似乎在不同的范围内运行
我找不到任何可帮助我使用协程创建测试的指南 预先感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)