如何测试碎片?

时间:2014-01-25 13:44:07

标签: android unit-testing testing android-fragments

抱歉我的英文

我开始学习如何在Android开发中使用TDD。

我需要孤立地测试Fragment。我使用了this tutorial,但是当我尝试提交事务(在fragment中添加activity)时,我遇到了异常。我找到了解决方案 - 使用transaction.commitAllowingStateLoss()代替transaction.commit()。但我不确定这个解决方案是否总能正常运行。

所以,现在我使用这样的东西:

在应用程序包(而不是测试包)中我有一个包helper_to_test_fragments,包含下一个类:

FragmentBuilder.java

public interface FragmentBuilder {
    Fragment build();
}

CurrentTestedFragmentBuilder.java

public class CurrentTestedFragmentBuilder {

    private static class DummyFragmentBuilder implements FragmentBuilder {
        @Override
        public Fragment build() {
            Fragment dummyFragment = new Fragment() {
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    // inflate empty FrameLayout
                    return inflater.inflate(R.layout.fragment_dummy, container, false);
                }
            };
            return dummyFragment;
        }
    }

    private static FragmentBuilder currentFragmentBuilder = new DummyFragmentBuilder();

    public static void setCurrentBuilder(FragmentBuilder fragmentBuilder) {
        if (fragmentBuilder == null) {
            throw new IllegalArgumentException("fragmentBuilder should be not null");
        }
        currentFragmentBuilder = fragmentBuilder;
    }

    public static Fragment build() {
        return currentFragmentBuilder.build();
    }
}

ActivityHelperToTestFragments.java

// This activity declared in application Manifest.xml file
public class ActivityHelperToTestFragments extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helper_to_test_fragments);

        if (savedInstanceState == null) {
            Fragment currentTestedFragment = CurrentTestedFragmentBuilder.build();
            getFragmentManager()
                .beginTransaction()
                .add(R.id.containerOfCurrentTestedFragment, currentTestedFragment)
                .commit();
        }
    }

    public Fragment getTestedFragment() {
        return getFragmentManager().findFragmentById(R.id.containerOfCurrentTestedFragment);
    }
}

activity_helper_to_test_fragments.xml - 上面帮助活动的布局

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/containerOfCurrentTestedFragment" />

是:

public class TestSomeFragment
         extends ActivityInstrumentationTestCase2<ActivityHelperToTestFragments> {

    private ActivityHelperToTestFragments activity;
    private Fragment testedFragment;

    public TestSomeFragment() {
        super(ActivityHelperToTestFragments.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        CurrentTestedFragmentBuilder.setCurrentBuilder(new FragmentBuilder() {
            @Override
            public Fragment build() {
                return new SomeFragment();
            }
        });
        activity = getActivity();
        getInstrumentation().waitForIdleSync();
        testedFragment = activity.getTestedFragment();
    }

    public void testPreconditions() {
        assertNotNull(activity);
    }

    public void testFragment() {
        assertNotNull(testedFragment);
    }

}

这种方法有哪些缺点?这种方法是否正确?在你看来,它有多容易?

我使用静态方法设置当前测试的片段。但是我使用这个解决方案来单独测试片段。

请建议比这更好的解决方案。

0 个答案:

没有答案