微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Spek与暂停方法一起使用

如何解决将Spek与暂停方法一起使用

我试图在我的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 举报,一经查实,本站将立刻删除。