【发布时间】:2019-01-02 04:10:20
【问题描述】:
所以到 Dagger 2.11 之前,我已经能够构建 TestComponent 和 Modules 以使关键组件能够被注入到集成测试中。这对于 Api 测试和具有大量组件要求的对象非常有用
通常我会有这样的代码:-
class SpotifyApiTest {
lateinit var spotifyApi : SpotifyApi
@Inject set
lateinit var spotifyHelper : SpotifyIOHelper
@Inject set
@Before
fun setup() {
var context = InstrumentationRegistry.getInstrumentation().context
val testAppComponent = DaggerSpotifyTestComponent.builder()
.spotifyApiModule(SpotifyApiModule(context))
.build()
testAppComponent.inject(this)
}
@Test
......
}
N/B remember to add the following to your gradle build file
kaptAndroidTest "com.google.dagger:dagger-compiler:$daggerVersion"
这种方法在 Dagger 2.11 之前工作得非常好,但在那个版本之后,带有参数化构造函数的模块无法阻止提供上下文,更不用说应用程序了。那么如何使用新的 AndroidInjection() 功能与 Dagger 2.16 进行集成测试呢?
【问题讨论】:
标签: android testing integration dagger