使用Dagger2当创建依赖性测试 [英] Creating test dependencies when using Dagger2

查看:214
本文介绍了使用Dagger2当创建依赖性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读文档的匕首2我无法找到一个简单的方法构建的测试应用程序时提供的依赖。我发现的唯一的线索是这样的:

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:

匕首2不支持替代。模块
  覆盖简单的测试假货可以创建
  该模块的一个子类来模拟该行为。
  使用覆盖,并依靠依赖注入模块
  应被分解,使得被覆盖的模块代替
  再presented作为一个选择两个模块之间。

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上,任何人都可以解释一下吗?

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

推荐答案

这是没有一些解决方法目前不可能用匕首2(V2.0.0作为的)。你可以阅读一下这里

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

我已经提议<一个href=\"https://stackoverflow.com/questions/26939340/how-do-you-override-a-module-dependency-in-a-unit-test-with-dagger-2-0/29996385#29996385\">one解决方法但是这需要改变生产code。

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

在短:


  • @Component 提供额外的制定者(例如在应用类的Andr​​oid二传手)

  • 测试组件必须扩大生产组件

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

有关详细信息,请检查这两个环节。希望这个问题将在2匕首的未来版本中得到解决。

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

这篇关于使用Dagger2当创建依赖性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆