【问题标题】:Is Robolectric able to assert that methods have been invoked?Robolectric 是否能够断言方法已被调用?
【发布时间】:2011-10-07 19:16:26
【问题描述】:

我的布局中定义了一个按钮,如下所示:

<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
    }
}

【问题讨论】:

    标签: android unit-testing android-2.1-eclair robolectric


    【解决方案1】:

    我从未使用过它们,但我相信您可以使用 TouchUtils 类来做到这一点。这是Android TouchUtils docs 的链接。特别是您应该查看clickView 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      相关资源
      最近更新 更多