我怎么能代替的活动范围依赖使用Dagger2嘲笑 [英] How can I replace Activity scoped dependencies with mocks using Dagger2

查看:213
本文介绍了我怎么能代替的活动范围依赖使用Dagger2嘲笑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动范围依赖,我想测试活动的一些嘲笑。我看了一下不同的方法,建议在测试过程中的测试元件取代应用程序组件,但我要的是更换活动的组成部分。

I have a scoped dependency in my Activity and I want to test that activity with some mocks. I have read about different approach that suggest to replace Application component with a test component during the test, but what I want is to replace the Activity component.

例如,我想测试对模拟presenter活动在我的MVP设置。

For example, I want to test the Activity against mock presenter in my MVP setup.

我相信,通过调用活动setComponent()更换组件将不会起作用,因为活动的依赖性通过字段注入已经注入,因此在测试过程中,真实的物体将被使用。

I believe that replacing component by calling setComponent() on Activity will not work, because Activity dependencies already injected via field injection, so during the test, real object will be used.

我怎样才能解决这个问题?什么Dagger1?是不是有同样的问题?

How can I resolve this issue? What about Dagger1? Is it has the same issue?

推荐答案

注射组件

首先,创建一个静态类作为一个工厂的活动。矿山看起来有点像这样:

First, you create a static class to act as a factory for your Activity. Mine looks a little like this:

public class ActivityComponentFactory {

    private static ActivityComponentFactory sInstance;

    public static ActivityComponentFactory getInstance() {
        if(sInstance == null) sInstance = new ActivityComponentFactory();
        return sInstance;
    }

    @VisibleForTesting
    public static void setInstance(ActivityComponentFactory instance) {
        sInstance = instance;
    }

    private ActivityComponentFactory() {
        // Singleton
    }

    public ActivityComponent createActivityComponent() {
        return DaggerActivityComponent.create();
    }
}

然后就去做 ActivityComponentFactory.getInstance()createActivityComponent()注资(本); 您的活动在

有关的测试,您可以替代你的方法了工厂,在创建活动之前。

For testing, you can replace the factory in your method, before the Activity is created.

提供了嘲笑

作为@ EpicPandaForce的回答表明,这样做的的官方支持的方式目前涉及到很多的样板和副本/粘贴code。匕首2队需要提供部分重写模块的简单的方法。

As @EpicPandaForce's answer makes clear, doing this the officially-supported way currently involves a lot of boilerplate and copy/pasted code. The Dagger 2 team need to provide a simpler way of partially overriding Modules.

直到他们这样做,虽然,这是我unnoficial方式:只需扩展模块

Until they do though, here's my unnoficial way: Just extend the module.

假设你想用一个模拟来取代你的ListView presenter。假设你有一个presenterModule看起来像这样:

Let's say you want to replace your ListViewPresenter with a mock. Say you have a PresenterModule which looks like this:

@Module @ActivityScope
public class PresenterModule {

    @ActivityScope
    public ListViewPresenter provideListViewPresenter() {
        return new ListViewPresenter();
    }

    @ActivityScope
    public SomeOtherPresenter provideSomeOtherPresenter() {
        return new SomeOtherPresenter();
    }
}

您可以做这在测试设置:

You can just do this in your test setup:

ActivityComponentFactory.setInstance(new ActivityComponentFactory() {
    @Override
    public ActivityComponent createActivityComponent() {
        return DaggerActivityComponent.builder()
                .presenterModule(new PresenterModule() {
                    @Override
                    public ListViewPresenter provideListViewPresenter() {
                        // Note you don't have to use Mockito, it's just what I use
                        return Mockito.mock(ListViewPresenter.class);
                    }
                })
                .build();
    }
});

...它的只是工作的!

请注意,您不必包括关于 @覆盖方法 @Provides 注释。事实上,如果你做那么匕首2 code代会失败。

Note that you don't have to include the @Provides annotation on the @Override method. In fact, if you do then the Dagger 2 code generation will fail.

这工作,因为该模块只是简单的工厂 - 生成组件类需要范围的情况下,缓存实例的照顾。在 @Scope 注释由code发电机使用,但在运行时不相关的。

This works because the Modules are just simple factories - the generated Component classes take care of caching instances of scoped instances. The @Scope annotations are used by the code generator, but are irrelevant at runtime.

这篇关于我怎么能代替的活动范围依赖使用Dagger2嘲笑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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