在 appComponent dagger 2 中动态添加测试模块? [英] Dynamically add test modules in appComponent dagger 2?

查看:53
本文介绍了在 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 中动态添加测试模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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