如何使用 Robolectric 测试片段?

2024-03-12

我知道有一个Robolectric.shadowOf(Fragment)方法和一个ShadowFragment类,认为它们没有在文档中列出,但我无法使其工作。

myFragment = new MyFragment();
myFragment.onCreateView(LayoutInflater.from(activity), (ViewGroup) activity.findViewById(R.id.container), null);
myFragment.onAttach(activity);
myFragment.onActivityCreated(null); 

我正在使用 API 级别 13(Honeycomb)。

Thanks.


编辑 #4 和 #5: In 机器人电动 3.* https://github.com/robolectric/robolectric/wiki/2.4-to-3.0-Upgrade-Guide,他们分割了片段启动函数。

对于支持片段,您需要添加依赖性 https://bintray.com/bintray/jcenter/org.robolectric%3Ashadows-supportv4/view给你的build.gradle:

testCompile "org.robolectric:shadows-supportv4:3.8"

Import: org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; http://robolectric.org/javadoc/3.0/org/robolectric/shadows/support/v4/SupportFragmentTestUtil.html

对于平台片段,您不需要这种依赖关系。进口:import static org.robolectric.util.FragmentTestUtil.startFragment; http://robolectric.org/javadoc/3.0/org/robolectric/util/FragmentTestUtil

他们都使用相同的名字startFragment().

import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class YourFragmentTest
{
    @Test
    public void shouldNotBeNull() throws Exception
    {
        YourFragment fragment = YourFragment.newInstance();
        startFragment( fragment );
        assertNotNull( fragment );
    }
}

Edit #3:Robolectric 2.4 有一个用于支持和常规片段的 API http://robolectric.org/javadoc/2.4/org/robolectric/util/FragmentTestUtil.html。您可以使用newInstance()模式或在构造你的时使用构造函数Fragment's.

import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertNotNull;
import static org.robolectric.util.FragmentTestUtil.startFragment;

@RunWith(RobolectricGradleTestRunner.class)
public class YourFragmentTest
{
    @Test
    public void shouldNotBeNull() throws Exception
    {
        YourFragment fragment = new YourFragment();
        startFragment( fragment );
        assertNotNull( fragment );
    }
}

Edit #2:如果您使用支持片段,则会有一个新的助手(支持常规活动/片段的应该在下一版本中 https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/util/FragmentTestUtil.java):

import static org.robolectric.util.FragmentTestUtil.startFragment;

@Before
public void setUp() throws Exception
{
    fragment = YourFragment.newInstance();
    startFragment( fragment );
}

Edit:如果您升级到 Robolectric 2.0:

public static void startFragment( Fragment fragment )
{
    FragmentActivity activity = Robolectric.buildActivity( FragmentActivity.class )
                                           .create()
                                           .start()
                                           .resume()
                                           .get();

    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add( fragment, null );
    fragmentTransaction.commit();
}

原答案

正如其他评论者所建议的,您确实需要使用片段管理器(而不是调用上面列出的生命周期方法)。

@RunWith(MyTestRunner.class)
public class YourFragmentTest
{
    @Test
    public void shouldNotBeNull() throws Exception
    {
        YourFragment yourFragment = new YourFragment();
        startFragment( yourFragment );
        assertNotNull( yourFragment );
    }

我创建了一个测试运行程序,并有一个为我启动片段的函数,这样我就可以在任何地方使用它。

public class MyTestRunner extends RobolectricTestRunner
{
    public MyTestRunner( Class<?> testClass ) throws InitializationError
    {
        super( testClass );
    }

    public static void startFragment( Fragment fragment )
    {
        FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add( fragment, null );
        fragmentTransaction.commit();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Robolectric 测试片段? 的相关文章

随机推荐