問題描述
您好,是否可以在我的 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 中動態添加測試模塊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!