【问题标题】:Having trouble testing RadioGroup with RoboGuice + Robolectric使用 RoboGuice + Robolectric 测试 RadioGroup 时遇到问题
【发布时间】:2012-04-08 13:50:22
【问题描述】:

我已经以多种不同的方式看待这个问题,而且我只剩下一点头发,我想我会把它放在那里,希望有人已经尝试过这个。

我正在尝试为启用 Roboguice 的 Activity 编写 Robolectric 测试。具体来说,我正在尝试编写确保 RadioGroup 行为的测试。

问题在于,在运行测试时,RadioGroup 的行为不像 RadioGroup 并强制执行一次只检查一个 RadioButton 的行为。我可以通过断言和调试器看到,我可以一次检查组中的所有三个按钮。

RadioGroup 非常简单:

<RadioGroup
    android:id="@+id/whenSelection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/whenToday"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/today" />

    <RadioButton
        android:id="@+id/whenYesterday"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yesterday" />

    <RadioButton
        android:id="@+id/whenOther"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/earlier" />

</RadioGroup>

然后我应该指出我运行应用程序的内容,其行为是我所期望的(如果我单击任何一个单选按钮,只有一个保持选中状态,而其他两个未选中)。所以,理论上,这个测试应该通过:

    -- snip--
    Assert.assertTrue(whenToday.isChecked());
    Assert.assertFalse(whenYesterday.isChecked());
    Assert.assertFalse(whenOther.isChecked());

    whenYesterday.performClick();

    Assert.assertTrue(whenYesterday.isChecked());
    Assert.assertFalse(whenToday.isChecked());
    -- snip --

但是,最后一个断言失败了,调试器确认第一个按钮 whenToday 保持选中状态。

这是完整的测试类:

@RunWith(InjectedTestRunner.class)
public class MyTest {
    @Inject ActivityLogEdit activity;

    RadioButton whenToday;
    RadioButton whenYesterday;
    RadioButton whenOther;

@Before
public void setUp() {
    activity.setIntent(new Intent());
    activity.onCreate(null);

    whenSelection = (RadioGroup) activity.findViewById(R.id.whenSelection);
    whenToday = (RadioButton) activity.findViewById(R.id.whenToday);
    whenYesterday = (RadioButton) activity.findViewById(R.id.whenYesterday);
    whenOther = (RadioButton) activity.findViewById(R.id.whenOther);       
}

@Test
public void checkDateSelectionInitialState() throws Exception {
    Assert.assertTrue(whenToday.isChecked());
    Assert.assertFalse(whenYesterday.isChecked());
    Assert.assertFalse(whenOther.isChecked());
    Assert.assertEquals(View.GONE, logDatePicker.getVisibility());

    whenYesterday.performClick();

    Assert.assertTrue(whenYesterday.isChecked());
    Assert.assertFalse(whenToday.isChecked());
  }
}

我已经尝试了所有我能想到的不同方式。我觉得我在做一些愚蠢的事情或缺少一些基本概念。请帮忙!

安德鲁

【问题讨论】:

    标签: android tdd radio-group roboguice robolectric


    【解决方案1】:

    我收到了其中一位开发者对 Robolectric Group 帖子的回复:

    您正在谈论的功能(选中单选按钮将 取消选中组中的所有其他单选按钮)尚未实现 在 Robolectric。随时提出功能请求: https://github.com/pivotal/robolectric/issues/ 如果我没记错,请尝试更改您的测试以使用 RadioGroup 上的 getCheckedRadioButtonId() 而不是 isChecked() RadioButtons——我相信它已经实现了。

    请注意,我也尝试过 getCheckedRadioButtonId(),但它也未实现(始终返回 -1)。

    安德鲁

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2016-11-02
      相关资源
      最近更新 更多