我是Espresso的新手,正在尝试在片段上编写测试,该片段在实例化时进行Retrofit调用。收到呼叫后,我想检查片段以查看是否存在视图。我正在使用IdlingResource作为该片段,并设置了要调用的侦听器,以在收到响应时将ResourceCallback转换为空闲状态(在此处执行以下一些步骤:https://stackoverflow.com/a/30820189/4102823)。

当用户登录并启动MainActivity时,将实例化我的片段。问题是我只是认为我的IdlingResource设置不正确,而且我也不知道它有什么问题。直到片段启动并进行调用之后,它才被构造,即使我先在测试的setUp()方法中注册IdlingResource,也要先进行其他操作。因此,我认为这里的主要问题是如何在运行测试时而不是在测试之后将IdlingResource与片段一起实例化。问题可能出在@Rule中吗?在测试可以在片段上运行之前,它是否启动MainActivity(创建NewsfeedFragment实例)?如果是这样,我该如何对片段使用规则?

这是我的片段:

public ProgressListener mProgressListener;
public boolean mIsProgressShown = true;

public interface ProgressListener {
    void onProgressShown();
    void onProgressDismissed();
}

public void setProgressListener(ProgressListener progressListener) {
    mProgressListener = progressListener;
}

public NewsfeedFragment() {
}

public static NewsfeedFragment newInstance() {
    return new NewsfeedFragment();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    OSUtil.setTranslucentStatusBar(getActivity().getWindow(), this.getContext());
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRootView = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_newsfeed, container, false);
    mNewsfeedFramelayout = (FrameLayout) mRootView.findViewById(R.id.newsfeed_framelayout);
    mProgressView = (ProgressBar) mRootView.findViewById(R.id.newsfeed_progress);
    mPageIndictorView = (FrameLayout) mRootView.findViewById(R.id.page_indicator);
    mPageIndicator = (TextView) mRootView.findViewById(R.id.page_indicator_text);
    mRootView.setOnRefreshListener(this);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) mRootView.findViewById(R.id.newsfeed_viewpager);
    mPageChangeListener = this;

    if (savedInstanceState != null) {
        mTabPosition = savedInstanceState.getInt(EXTRA_TAB_POSITION);
        mViewPager.setCurrentItem(mTabPosition);
    }

    fetchNewsFeed();

    return mRootView;
}

private void fetchNewsFeed() {
    if (NetworkUtils.isConnectedToInternet(getActivity())) {
        if (NetworkUtils.getService() != null) {
            if (mRootView.isRefreshing()) {
                dismissProgress();
            } else {
                showProgress();
            }
            showProgress();

            Call<Newsfeed> call = NetworkUtils.getService().getNewsfeed();
            call.enqueue(new Callback<Newsfeed>() {
                @Override
                public void onResponse(Call<Newsfeed> call, Response<Newsfeed> response) {
                    if (response.isSuccessful()) {

                        dismissProgress();
                        mNewsfeed = response.body();
                        FragmentManager fragmentManager = getChildFragmentManager();
                        mNewsfeedPagerAdapter = new NewsfeedPagerAdapter(fragmentManager, mNewsfeed.getNewsfeedItems());
}

...


    private void showProgress() {
        // show the progress and notify the listener
        if (mProgressListener != null){
            setProgressIndicatorVisible(true);
            notifyListener(mProgressListener);
        }
    }

    public void dismissProgress() {
        // hide the progress and notify the listener
        if (mProgressListener != null){
            setProgressIndicatorVisible(false);
            mIsProgressShown = false;
            notifyListener(mProgressListener);
        }
    }

    public boolean isInProgress() {
        return mIsProgressShown;
    }

    private void notifyListener(ProgressListener listener) {
        if (listener == null){
            return;
        }
        if (isInProgress()){
            listener.onProgressShown();
        }
        else {
            listener.onProgressDismissed();
        }
    }


这是IdlingResource:

public class ProgressIdlingResource implements IdlingResource, NewsfeedFragment.ProgressListener {

private ResourceCallback mResourceCallback;
private NewsfeedFragment mNewsfeedFragment;


public ProgressIdlingResource(NewsfeedFragment fragment) {
    mNewsfeedFragment = fragment;
    mNewsfeedFragment.setProgressListener(this);
}

@Override
public String getName() {
    return "ProgressIdlingResource";
}

@Override
public boolean isIdleNow() {
    return !mNewsfeedFragment.mIsProgressShown;
}

@Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
    mResourceCallback = callback;
}

@Override
public void onProgressShown() {

}

@Override
public void onProgressDismissed() {
    if (mResourceCallback == null) {
        return;
    }
    //Called when the resource goes from busy to idle.
    mResourceCallback.onTransitionToIdle();
}
}


片段测试:

public class NewsfeedFragmentTest {
@Before
public void setUp() throws Exception {
    Espresso.registerIdlingResources(new ProgressIdlingResource((NewsfeedFragment) MainActivity.getCurrentFragment()));
}

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void getViewPager() throws Exception {
    onView(allOf(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
            withId(R.id.newsfeed_viewpager))).check(matches(isDisplayed()));
}

@Test
public void getNewsfeedItems() throws Exception {
    onView(withId(R.id.page_indicator)).check(matches(isDisplayed()));
}


}

最佳答案

改造使用OkHttp,并且有一种标准的方法可以设置IdlingResource。请参阅OkHttp IdlingResource

10-08 06:36