我在使用espresso意图来测试使用意图启动的活动时遇到了麻烦.执行流程以下列方式工作
创建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 …