You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mockito测试Preferences DataStore的isWizardComplete方法

问题分析

原测试的核心问题是错误地mock了map操作后的Flow,而不是先mockdataStore.data本身。InfoWizardDataSourceImpl初始化时就会基于dataStore.data创建stateFlow,如果dataStore.data没有被正确mock,stateFlow就无法获取到有效数据,导致调用first()时出现异常或返回错误值。

修正方案
  1. 先mockdataStore.data,返回一个包含目标Preferences的Flow,用preferencesOf创建携带IS_WIZARD_COMPLETE键值的Preferences实例。
  2. 确保在测试协程环境中执行挂起函数,使用你自定义的runCoroutineTest或官方的runTest。
修正后的测试代码
class InfoWizardDataSourceImplTest : BaseCoroutinesTest() {

    private val dataStore = mock(DataStore::class.java) as DataStore<Preferences>
    private val infoWizardDataSourceImpl = InfoWizardDataSourceImpl(dataStore)
    private val IS_WIZARD_COMPLETE = booleanPreferencesKey("isWizardComplete")

    @Test
    fun `isWizardComplete returns true when key is set to true`() = runCoroutineTest {
        // 创建包含指定键值的Preferences实例
        val testPreferences = preferencesOf(IS_WIZARD_COMPLETE to true)
        // mock dataStore.data 返回携带该实例的Flow
        `when`(dataStore.data).thenReturn(flowOf(testPreferences))

        val result = infoWizardDataSourceImpl.isWizardComplete()
        assertTrue(result)
    }

    @Test
    fun `isWizardComplete returns false when key does not exist`() = runCoroutineTest {
        // 创建不包含目标键的空Preferences实例
        val testPreferences = preferencesOf()
        `when`(dataStore.data).thenReturn(flowOf(testPreferences))

        val result = infoWizardDataSourceImpl.isWizardComplete()
        assertFalse(result)
    }
}
额外注意事项
  • 确保BaseCoroutinesTest已正确配置测试协程调度器,避免挂起函数执行时出现异常。
  • 测试覆盖了两种核心场景:键存在且为true、键不存在(返回默认false),保证方法逻辑的完整性。

内容的提问来源于stack exchange,提问作者TOvchinnikova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:15:38