Creating test dependencies when using Dagger2

送分小仙女□ 提交于 2019-12-08 15:46:49

问题


While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this:

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.

I don't understand how I would set up such a configuration on Android, anyone can explain?


回答1:


This is currently impossible with Dagger 2 (as of v2.0.0) without some workarounds. You can read about it here.

I've proposed one workaround but this requires changes to the production code.

In short:

  • provide additional setter for @Component (e.g. in Android setter in Application class)
  • test component must extend the production component

For more information please check both links. Hope this issue will be addressed in future versions of Dagger 2.



来源:https://stackoverflow.com/questions/29969913/creating-test-dependencies-when-using-dagger2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!