gpt4 book ai didi

android - 在 Robolectric 中使用 savedInstanceState Bundle 测试 Fragment 的 onCreate 方法?

转载 作者:太空狗 更新时间:2023-10-29 15:07:09 25 4
gpt4 key购买 nike

我有一个带有 onCreate 方法的 Fragment,它使用传入的 savedInstanceState Bundle 做一些事情。我正在使用 Robolectric 创建一个 Activity 并使用 Activity 的 FragmentManager 启动 Fragment。

我遇到的问题是我不知道如何从测试代码中插入一个 savedInstanceState Bundle 来验证使用它的代码路径。

我希望 Robolectric 有一个影子 fragment ,可以让我直接设置 bundle 。

有什么想法吗?

最佳答案

如果您使用 Robolectric 2.x,那么您可以将 Bundle 传递给 Activity :

Bundle savedBundle = new Bundle();

activity = buildActivity( FragmentActivity.class ).create( savedBundle ).start().resume().get();

MyFragment fragment = new MyFragment();

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

关于android - 在 Robolectric 中使用 savedInstanceState Bundle 测试 Fragment 的 onCreate 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20851035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com