简体   繁体   English

使用 FragmentScenario 测试片段时如何测试与菜单的交互

[英]How to test interactions with menu when testing fragments with FragmentScenario

I'm trying to test a fragment using FragmentScenario.我正在尝试使用 FragmentScenario 测试片段。 This fragment has its own menu.这个片段有自己的菜单。 There is an add icon on the action bar and clicking on this menu item launches a child fragment from which user can add new items.操作栏上有一个添加图标,单击此菜单项会启动一个子片段,用户可以从中添加新项目。 So I'm trying to test this behavior.所以我正在尝试测试这种行为。 However, as you may know, FragmentScenario launches the fragment within an EmptyFragmentActivity, without launching the real host activity.但是,您可能知道,FragmentScenario 在 EmptyFragmentActivity 中启动片段,而不启动真正的宿主活动。 As action bar is not a part of the fragment layout but belongs to the host activity, action bar and thus menu doesn't even become visible during testing.由于操作栏不是片段布局的一部分,而是属于宿主活动,因此操作栏和菜单在测试期间甚至不可见。 So how can I test interactions with the menu?那么如何测试与菜单的交互呢?

I found this piece of information from the official docs:我从官方文档中找到了这条信息:

If you need to call a method on the fragment itself, such as responding to a selection in the options menu, you can do so safely by implementing FragmentAction:如果您需要调用片段本身的方法,例如响应选项菜单中的选择,您可以通过实现 FragmentAction 来安全地执行此操作:

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
    @Test fun testEventFragment() {
        val scenario = launchFragmentInContainer<MyFragment>()
        scenario.onFragment(fragment ->
            fragment.onOptionsItemSelected(clickedItem) {
                //Update fragment's state based on selected item.
            }
        }
    }
}

However, how to pass the correct item to the onOptionsItemSelected callback?但是,如何将正确的项目传递给 onOptionsItemSelected 回调? I tried to define addMenuItem as a member variable and initialize it inside onCreateOptionsMenu, but it returns null. onCreateOptionsMenu does not seem to be called during testing.我尝试将addMenuItem定义为成员变量,并在onCreateOptionsMenu内部初始化,结果返回null。测试时似乎没有调用onCreateOptionsMenu。 So I don't know how to test user interactions with the menu.所以我不知道如何测试用户与菜单的交互。

I solved the issue by passing a dummy menu item:我通过传递一个虚拟菜单项解决了这个问题:

val scenario = launchFragmentInContainer<CategoryListFragment>(Bundle(), R.style.AppTheme)

//Create a dummy menu item with the desired item id.
val context: Context = ApplicationProvider.getApplicationContext<AndroidTestApplication>()
val addMenuItem = ActionMenuItem(context, 0, R.id.action_add, 0, 0, null)
 
 //Call onOptionsItemSelected with the dummy menu item
 scenario.onFragment { fragment ->
       fragment.onOptionsItemSelected(addMenuItem)
 }

EDIT : This solution saved the day at that time.编辑:这个解决方案在当时挽救了一天。 But now I began to prefer to put the toolbar in the fragment layout instead of activity, especially if I have different menus for different fragments, so I that I don't have the same problem in other projects.但是现在我开始更喜欢将工具栏放在片段布局中而不是活动中,尤其是如果我为不同的片段设置了不同的菜单,那么我在其他项目中就不会遇到同样的问题。 That's a possible solution as well.这也是一个可能的解决方案。

You can also mock the menu item using mockito. Something like this:您还可以使用 mockito 模拟菜单项。像这样:

val menuItemMock = mock<ActionMenuItem> {
   on { itemId } doReturn R.id.action_item
}

       
launchFragmentInContainer { YourFragment().also{ /*initialize*/ } }
.onFragment {
    it.onOptionsItemSelected(menuItemMock)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 FragmentScenario 对 Dagger Fragments 进行单元测试 - Unit test Dagger Fragments with FragmentScenario 如何使用 FragmentScenario 测试 DaggerFragment? - How to test DaggerFragment with FragmentScenario? 必须使用父活动引用时,如何使用“FragmentScenario”单独测试 Fragment - How to test Fragment in isolation using `FragmentScenario` when you have to use parent activity reference 使用 FragmentScenario 进行仪器测试 - Instrumental Testing with FragmentScenario Hilt Fragments 必须附加到 @AndroidEntryPoint Activity。 找到:class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity - Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity 如何从测试 class 获取属性? FragmentScenario,浓缩咖啡 - How to get access to a property from the test class? FragmentScenario, Espresso 动态功能模块中 Fragments 的 fragmentScenario Espresso 测试 - fragmentScenario Espresso tests of Fragments in Dynamic feature modules 使用 FragmentScenario 时的样式问题 - Styling issue when using FragmentScenario 如何在使用 FragmentScenario 的 Android 仪器测试中预期错误? - How can I expect errors in my Android instrumentation test using FragmentScenario? 如何在片段中制作菜单 - How to make a Menu inside Fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM