小编une*_*ewk的帖子

测试无法匹配正确的意图组件

我在使用espresso意图来测试使用意图启动的活动时遇到了麻烦.执行流程以下列方式工作

  1. 在主活动中,单击一个按钮
  2. 该按钮执行AsyncTask以获取一些数据
  3. 在AsyncTask的onPostExecute中,我创建了一个intent add fetched data作为Intent Extra,然后启动一个显示所获取数据的新活动.

创建intent并启动新活动的代码如下所示:

@Override
protected void onPostExecute(String result) {
   Intent intent = new Intent(context, NewActivity.class);
   intent.putExtra(key, result);
   context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

这一切都在我运行应用程序时有效.至于使用espresso进行测试,测试代码如下所示:

...

@Rule
public IntentsTestRule<MainActivity> mIntentTestRule =
                           new IntentsTestRule<>(MainActivity.class, false);

@Test
public void testMainActivityAsyncTask() {
    // Find view with button
    onView(withText(R.string.button_text)).perform(click());

    // Ensure correct intent setup
    intended(allOf(
            hasComponent(NewActivity.class.getName()),
            hasExtraWithKey(key),
            toPackage("com.example.espresso")));

    // Ensure appropriate activity and view is launched
    onView(
            allOf(withId(R.id.expected_view),                                                   
            withText(isEmptyOrNullString())));

    // Let it go
    Intents.release();
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,测试失败并出现这些错误(为简洁起见):

...想要匹配1个意图.实际上匹配0意图.

匹配意图:[]

记录的意图:-Intent {cmp = com.example.doingstuff/com.example.espresso.NewActivity(has extras)}处理包:[[com.example.doingstuff]],extras:[Bundle …

android android-testing android-espresso

6
推荐指数
0
解决办法
635
查看次数