在活动中注入ViewModelFactory.Provider以进行浓缩咖啡测试 [英] Inject ViewModelFactory.Provider in activity for espresso testing

查看:90
本文介绍了在活动中注入ViewModelFactory.Provider以进行浓缩咖啡测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题: Espresso,Dagger2在BaseActivity上设置了ViemodelProvider.Factory

在测试过程中,我经历了一次苦难,然后返回了一个Activity上的ViewModelFactory.Provider,目的是使意式浓缩咖啡测试适用于Android体系结构组件。我以为它很简单,但是我想这不是...

I went through hell and back to get a ViewModelFactory.Provider on an Activity during my tests, in order to get espresso tests working regarding Android Architecture Components. I expected it to be simple, but I guess it's not...

使它与片段一起工作的示例很简单:

https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/user/UserFragmentTest。 java

The example to get it working with fragments is straightforward:
https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/user/UserFragmentTest.java

@Before
public void init() {
    UserFragment fragment = UserFragment.create("foo");
    viewModel = mock(UserViewModel.class);
    when(viewModel.getUser()).thenReturn(userData);
    when(viewModel.getRepositories()).thenReturn(repoListData);
    navigationController = mock(NavigationController.class);
    fragmentBindingAdapters = mock(FragmentBindingAdapters.class);

    fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
    fragment.navigationController = navigationController;
    fragment.dataBindingComponent = () -> fragmentBindingAdapters;

    activityRule.getActivity().setFragment(fragment);
}

但是,这根本无法用于活动,因为我无法获得使用 ActivityTestRule 创建活动之前,对活动的依赖关系。

我使用Dagger2遵循与上面示例中相同的新依赖项注入流程,使用 HasActivityInjector 接口。

However, this simply won't work with activities as I can't get the dependencies on the activity before its creation using an ActivityTestRule.
I followed the same new dependency injection flow with Dagger2 as in the example above using the HasActivityInjector interface.

感谢您的帮助!

推荐答案

可以通过在@Before方法中的TestApp中注册自定义ActivityLifecycleCallbacks来设置注入的活动属性。

It’s possible to set injected activity attribute by registering a custom ActivityLifecycleCallbacks in your TestApp in the @Before Method.

示例:

 @Before
public void init(){


    UserFragment fragment = UserFragment.create("foo");
    viewModel = mock(UserViewModel.class);
    when(viewModel.getUser()).thenReturn(userData);
    when(viewModel.getRepositories()).thenReturn(repoListData);
    navigationController = mock(NavigationController.class);


    TestApp testApp = ((TestApp) InstrumentationRegistry.getContext().getApplicationContext());
    testApp.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            //will be called before the onCreate method of your activity
            activity.setViewModelFactory(ViewModelUtil.createFor(viewModel));
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });


    fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
    fragment.navigationController = navigationController;
    fragment.dataBindingComponent = () -> fragmentBindingAdapters;

    activityRule.getActivity().setFragment(fragment);


}

这篇关于在活动中注入ViewModelFactory.Provider以进行浓缩咖啡测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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