我已经看了很多不同的方式,我留下了什么小头发,我想我会把它放在那里,希望有人已经尝试过这个。
我正在尝试编写我的Roboguice启用的活动的Robolectric测试。具体来说,我正在尝试编写确保RadioGroup行为的测试。
问题在于,当运行测试时,RadioGroup不像RadioGroup那样行使并强制执行一次一个RadioButton检查的行为。我可以通过Asserting和调试器看到我可以同时检查组中的所有三个按钮。
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 --
但是,最后一个断言失败了,调试器确认第一个按钮在今天仍然被检查。
以下是完整的测试类:
@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());
}
}
我已经尝试过每一种我能想到的不同方式。我感觉我做了一些愚蠢或缺少一些基本概念。请帮忙!
安德鲁
答案 0 :(得分:1)
我从其中一个开发它的人那里得到了一个关于Robolectric集团的帖子的答案:
您正在谈论的功能(检查单选按钮会 取消选中组中的所有其他单选按钮)尚未实现 在Robolectric。随意提交功能请求: https://github.com/pivotal/robolectric/issues/ 如果我没记错的话,请尝试更改要使用的测试 RadioGroup上的getCheckedRadioButtonId()而不是isChecked() RadioButtons - 我相信已经实现了。
请注意,我也尝试了getCheckedRadioButtonId()
并且它也未实现(始终返回-1)。
安德鲁