模拟上下文中的 FilesDir 为空

FilesDir on mocked context Is null

我是单元测试的新手,目前正在为我的 Kotlin android 应用程序使用 Mockito。

我在我的视图模型中使用 Koin 进行依赖注入 class。我已经成功地模拟了依赖项。我将 mockedContext 传递给我的 viewModel class 但是在调用 mockedContext.filesDir 时它 returns 为空。

我是不是漏掉了什么?

HomeViewModelClass

init {
    directoryManager.createDirectory()
}

目录管理器

class DirectoryManager(val context:Context){

    fun createDirectory(){
        val filePath: String = context.filesDir.absolutePath
    }
}

HomeViewModelTest

@Mock
private lateinit var mockedContext: Context
private lateinit var homeViewModel: HomeViewModel

@Before
fun setup() {
    homeViewModel = HomeViewModel(mockedContext)
}

在 DirectoryManager

中获取 context.filesDir 的空值

我的解决方案是改用 mockedApplication!