Robolectric 是否能够断言方法已被调用?
我在布局中定义了一个按钮,如下所示:
<Button
android:id="@+id/speakButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/speak"
android:onClick="speak"
/>
onClick
绑定到我的活动中的方法,例如:
public void speak(View v)
{
// Do my stuff here
}
使用 Robolectric,我可以为该活动创建一个简单的测试类,我想知道我是否可以进行一个调用按钮的测试,并确保我的活动中的方法被正常调用。
(我的应用程序中有一大堆按钮,因此打算进行测试以确保它们正确连接,除非有人对我为什么不应该打扰有任何建议)
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest
{
private MyActivitymActivity;
private Button speakButton;
@Before
public void setUp() throws Exception
{
mActivity = new MyActivity();
mActivity.onCreate(null);
speakButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakButton);
}
@Test
public void testButtonsVisible()
{
assertThat(speakButton.getVisibility(), equalTo(View.VISIBLE));
}
@Test
public void buttonsInvokeIntendedMethods()
{
// Unsure how to implement this test
}
}
I've got a button defined in my layout as follows :
<Button
android:id="@+id/speakButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/speak"
android:onClick="speak"
/>
The onClick
binds to a method in my activity, such as :
public void speak(View v)
{
// Do my stuff here
}
Using Robolectric, I'm able to create a simple test class for that activity, I'd like to know if its possible that I could have a test that invokes the button, and ensures the method in my activity was invoked OK.
(I've got a whole bunch of buttons throughout my app, so intending to have tests to ensure they are wired up correctly, unless anyone has any suggestions as to why I shoudln't bother)
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest
{
private MyActivitymActivity;
private Button speakButton;
@Before
public void setUp() throws Exception
{
mActivity = new MyActivity();
mActivity.onCreate(null);
speakButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakButton);
}
@Test
public void testButtonsVisible()
{
assertThat(speakButton.getVisibility(), equalTo(View.VISIBLE));
}
@Test
public void buttonsInvokeIntendedMethods()
{
// Unsure how to implement this test
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未使用过它们,但我相信您可以使用 TouchUtils 类来做到这一点。以下是 Android TouchUtils 文档 的链接。您尤其应该查看
clickView
方法。I've never used them but I believe you can do this with the
TouchUtils
class. Here is a link to the Android TouchUtils docs. In particular you should look at theclickView
method.