如何用Robolectric测试碎片?

问题描述 投票:56回答:6

我知道有一个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)。

谢谢。

android robolectric
6个回答
107
投票

编辑#4&#5:在Robolectric 3.*中,他们拆分片段启动函数。

对于支持片段,您需要在dependency中添加build.gradle

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

进口:org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;

对于平台片段,您不需要此依赖项。进口:import static org.robolectric.util.FragmentTestUtil.startFragment;

它们都使用相同名称的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 );
    }
}

编辑#3:Robolectric 2.4有一个API for support and regular fragments。您可以使用newInstance()模式,也可以在构造Fragment时使用构造函数。

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 );
    }
}

编辑#2:如果您正在使用支持片段(one that supports regular activities/fragments should be in the next release),那么有一个新助手:

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

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

编辑:如果您升级到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();
    }
}

36
投票

你们这些都是艰难的做法。只需使用FragmentTestUtil。

FragmentTestUtil.startFragment(yourfragment);

13
投票

支持片段已移至模块:

阴影支持-V4

(截至2015年7月,Robolectric v3.0)

向app / build.gradle添加gradle依赖项:

testCompile 'org.robolectric:shadows-support-v4:3.0'

然后导入到你的Robolectric测试java类:

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

然后,您可以启动并使用support-v4片段进行测试:

@Test
public void minimalFragmentTest() throws Exception {
    MyFunFragment fragment = new MyFunFragment();
    SupportFragmentTestUtil.startVisibleFragment(fragment);
    assertThat(fragment.getView()).isNotNull();
}

参考文献:


2
投票

我很确定你必须使用FragmentTransaction创建一个FragmentManager,然后它会工作。


2
投票

我只想在Robolectric 2.0中添加它,即使在执行了以下操作之后:

activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get();
fragment.show(activity.getSupportFragmentManager(), null);
fragment.getDialog();  //This stills returns null

它仍然为我返回null。我做的是添加activity.getSupportFragmentManager().executePendingTransaction();,它的工作原理。

似乎robolectric由于某种原因不能运行它。似乎可能是Looper暂停了什么的。这对我有用,它看起来像这样:

activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get();
fragment.show(activity.getSupportFragmentManager(), null);
activity.getSupportFragmentManager().executePendingTransactions();
fragment.getDialog();

1
投票
SupportFragmentTestUtil.startFragment(fragment, AppCompatActivity::class.java)

如果活动正在扩展AppCompatActivity

这是使用Kotlin

© www.soinside.com 2019 - 2024. All rights reserved.