问题
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 inApplication
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