可以在我的AppComponent中添加测试模块吗?
Hi is it possible to add test modules in my AppComponent?
下面是我对appComponent的真实表示
Below is my real representation of my appComponent
@Singleton @Component(modules = arrayOf(MainModule::class, AnalyticsModule::class, MainAndroidBinding::class, AccountAndroidBinding::class, AndroidSupportInjectionModule::class, HomeAndroidBinding::class, NetworkModule::class)) interface ApplicationComponent : AndroidInjector<DaggerApplication> { fun inject(myApplication: MyApplication) override fun inject(instance: DaggerApplication) @Component.Builder interface Builder { @BindsInstance fun application(applicaton: Application): Builder fun build(): ApplicationComponent } }我可以像这样直接将测试模块添加到testAppComponent,但是它不能为我提供很大的灵活性来动态添加不同的testModules.
I could just add the test modules directly to the testAppComponent like this but it doesnt offer me much flexibility to dynamically add different testModules.
@Singleton @Component(modules = [ (MainModuleTest::class), (TestMainAndroidBindingTest::class), (AccountAndroidBindingTest::class), (AnalyticsModuleTest::class), (AndroidSupportInjectionModule::class), (NetworkModuleTest::class)]) interface TestAppComponent : ApplicationComponent { fun inject(launchActivityTest: LaunchActivityTest) @Component.Builder interface Builder { @BindsInstance fun application(applicaton: Application): Builder fun build(): TestAppComponent } }这是我的MyApplication类
Here is my MyApplication class
class MyApplication : DaggerApplication() { companion object { private lateinit var application: MyApplication fun get(): MyApplication { return application } } @Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity> lateinit var applicationComponent: ApplicationComponent override fun onCreate() { super.onCreate() application = this } override fun applicationInjector(): AndroidInjector<out DaggerApplication> { applicationComponent = DaggerApplicationComponent.builder() .application(this) .build() applicationComponent.inject(this) return applicationComponent } override fun attachBaseContext(base: Context?) { super.attachBaseContext(base) MultiDex.install(this) } }在LaunchActivityTest上,这是我将其设置为使用此testApp组件的方式
On the LaunchActivityTest this is how i set it up to use this testApp component
@Before fun setUp() { val app = InstrumentationRegistry.getTargetContext().applicationContext as MyApplication val testAppComponent = DaggerTestAppComponent.builder().application(app).build() app.applicationComponent = testAppComponent testAppComponent.inject(this) }我一直遵循本指南,直到偶然发现我的DaggerTestAppComponent并未公开我要动态添加自己的模块,因为我的AppComponent类扩展了AndroidInjector并自动为您添加了模块
I was following this guide until i stumbled o the point where my DaggerTestAppComponent doesnt expose the modules for me to dynamically add myself due to the fact that my AppComponent class extends AndroidInjector which automatically adds the modules for you
proandroiddev/writing-espresso- Instrumentation-tests-with-dagger2-kotlin-d30f12c4769b
上面的代码动态地添加了它的模块,如下所示:
The above dynamically adds its modules like this:
testAppComponent = DaggerTestAppComponent.builder() .appModule(AppModule(app)) .apiModule(TestApiModule()) .prefModule(TestPrefModule()) .build()除非我重做我的AppComponent以便它不扩展AndroidInjector,否则我无法这样做.如果我这样做了,那么在我真正的隐含代码中,我必须手动设置模块.
I cant do that in my case unless i redo my AppComponent so that it doesnt extend AndroidInjector. If i do that then in my real impl code i have to manually set the modules.
还有其他方法吗?
推荐答案您应在组件构建器中添加一个函数,并使用"BindsInstance".
You should add a function to your component builder and use "BindsInstance".
示例组件
@Singleton @Component(modules = { AndroidSupportInjectionModule.class, ApplicationTestModule.class, ActivityBuilder.class}) public interface TestExampleComponent extends AndroidInjector<DaggerApplication> { void inject(TestApplication app); @Override void inject(DaggerApplication instance); @Component.Builder interface Builder { @BindsInstance TestExampleComponent.Builder application(DaggerApplication application); Builder applicationModule(ApplicationTestModule appTestModule); TestExampleComponent build(); } }在此组件中,我使用"BindsInstance"添加了applicationModule函数,并且可以传递ApplicationTestModule.
In this component, I added applicationModule function with using "BindsInstance" and I can pass ApplicationTestModule.
然后您可以添加不同的测试模块.
Then you can add different test modules.
TestApplicationComponent appComponent = DaggerTestAppComponent.builder().application(this). applicationModule(appTestModule).build(); appComponent.inject(this);更多推荐
在appComponent匕首2中动态添加测试模块?
发布评论