如何测试片段,Robolectric? [英] How can I test fragments with Robolectric?
问题描述
我知道有一个 Robolectric.shadowOf(片段)
方法和 ShadowFragment
类,认为他们AREN'牛逼上市的文档,但我不能使它工作。
I know there is a Robolectric.shadowOf(Fragment)
method and a ShadowFragment
class, thought they aren't listed on the docs, but I can't make it work.
myFragment = new MyFragment();
myFragment.onCreateView(LayoutInflater.from(activity), (ViewGroup) activity.findViewById(R.id.container), null);
myFragment.onAttach(activity);
myFragment.onActivityCreated(null);
我正与API级13(蜂窝)。
I'm working with API level 13 (Honeycomb).
感谢。
推荐答案
编辑#3 :Robolectric 2.4具有的API支持和定期的碎片的。您可以施工时使用的newInstance()
图案或使用构造你的片段
的。
Edit #3: Robolectric 2.4 has an API for support and regular fragments. You can either use the newInstance()
pattern or use the constructor when constructing your 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 DisplayFragmentTest
{
@Test
public void shouldNotBeNull() throws Exception
{
DisplayFragment fragment = new DisplayFragment();
startFragment( fragment );
assertNotNull( fragment );
}
}
编辑#2 :有,如果您使用的是支持的片段(是一个新帮手<一href="https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/util/FragmentTestUtil.java">one支持常规活动/片段应在下一版本):
Edit #2: There's a new helper if you're using support fragments (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 = DetailFragment.newInstance( createVeggieDetail() );
startFragment( fragment );
}
修改:如果您升级到Robolectric 2.0:
Edit: If you upgraded to 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();
}
原来的答复
由于其他评论者建议,你需要使用(而不是打电话给你上面列出的生命周期方法)的片段经理。
As the other commenter suggested, you do need to use the fragment manager (instead of calling the lifecycle methods you listed above).
@RunWith(MyTestRunner.class)
public class MyFragmentTest
{
@Test
public void shouldNotBeNull() throws Exception
{
MyFragment myFragment = new MyFragment();
startFragment( myFragment );
assertNotNull( myFragment );
}
我创建了一个测试运行,并有一个启动一个片段,我的函数,所以我可以用它无处不在。
I create a test runner and have a function that starts up a fragment for me so I can use it everywhere.
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();
}
}
这篇关于如何测试片段,Robolectric?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!