在阅读 dagger 2 的文档时,我找不到在构建用于测试的应用程序时提供依赖项的简单方法。我发现的唯一线索是:
Dagger 2 doesn't support overrides. Modules that override for simple testing fakes can create a subclass of the module to emulate that behaviour. Modules that use overrides and rely on dependency injection should be decomposed so that the overridden modules are instead represented as a choice between two modules.
我不明白如何在 Android 上设置这样的配置,谁能解释一下?
最佳答案
如果没有一些变通办法,这对于 Dagger 2(从 v2.0.0 开始)目前是不可能的。你可以阅读它here .
我提议了one workaround但这需要更改生产代码。
简而言之:
- 为
@Component
提供额外的 setter(例如,在Application
类的 Android setter 中) - 测试组件必须扩展生产组件
有关更多信息,请检查这两个链接。希望这个问题会在 Dagger 2 的 future 版本中得到解决。
关于android - 使用 Dagger2 时创建测试依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29969913/