本文介绍了Espresso,Dagger2在BaseActivity上设置了ViemodelProvider.Factory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个抽象的AccountRequiredActivity,它看起来像这样(并且工作正常):

I have an abstract AccountRequiredActivity that looks like this (and works fine):

public abstract class AccountRequiredActivity extends LifecycleActivity {

    @Inject
    ViewModelProvider.Factory viewModelFactory;

    private AccountViewModel accountViewModel;

    public abstract void doOnCreate(Bundle savedInstanceState);
    public abstract void doOnResume();

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_app);
        AndroidInjection.inject(this);
        accountViewModel = ViewModelProviders.of(this, viewModelFactory).get(AccountViewModel.class);

        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnCreate(savedInstanceState);
        };

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnResume();
        };
    }

    private void redirectToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}

测试期间的问题是我无法在活动上设置viewModelFactory.

The problem during tests is that there is no way for me to set the viewModelFactory on the activity.

当活动有片段时,我可以做类似的事情:

When an activity has a fragment, I can just do something like:

@Before
public void init() {
    LoginFragment fragment = LoginFragment.newInstance();
    viewModel = mock(AccountViewModel.class);
    when(viewModel.getAuthenticatedUserResource()).thenReturn(authenticatedUser);

    fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
    activityRule.getActivity().setFragment(fragment);
}

但是,在这种情况下的问题是我在测试中使用了此方法(HomeActivity扩展了AccountRequiredActivity):

The problem however in this case is that I use this in my tests (HomeActivity extends AccountRequiredActivity):

@Rule
public ActivityTestRule<HomeActivity> activityTestRule = new ActivityTestRule<>(HomeActivity.class, true, false);

因此,由于onCreate立即被调用,因此无法动态设置viewModelFactory.似乎没有一种方法可以在调用onCreate之前获得对Activity对象的访问.

So there is no way to dynamically set the viewModelFactory, as onCreate immediately gets called. There doesn't seem to be a way to get access to the Activity object before onCreate gets called.

如何解决此问题?

注意:我将Dagger 2.11与AndroidInjector一起使用.
另请参阅我昨天发布的有关后续信息的问题:

Note: I use Dagger 2.11 with AndroidInjector.
Also see this question that I posted yesterday for follow-up info:

将ViewModelFactory.Provider注入活动中以进行浓缩咖啡测试

推荐答案

我通过重写AndroidInjector的inject()方法解决了该问题:

I solved the problem by overriding AndroidInjector's inject() method:

@Override
public AndroidInjector<Activity> activityInjector() {
    return new AndroidInjector<Activity>() {
        @Override
        public void inject(Activity instance) {
            AccountViewModel viewModel = mock( AccountViewModel.class );
            if(instance instanceof TestHomeActivity) {
                ((TestHomeActivity) instance).viewModelFactory = ViewModelUtil.createFor( viewModel );
            }
        }
    };
}

这篇关于Espresso,Dagger2在BaseActivity上设置了ViemodelProvider.Factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 15:00