Dynamically add test modules in appComponent dagger 2?(在 appComponent dagger 2 中动态添加测试模块?)
问题描述
您好,是否可以在我的 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
https://proandroiddev.com/writing-espresso-仪器测试-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.如果我这样做,那么在我真正的 impl 代码中我必须手动设置模块.
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 dagger 2 中动态添加测试模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 appComponent dagger 2 中动态添加测试模块?


基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01