Unit testing robolectric,没有与“receiveResult”匹配的意图`
请参阅我的代码:Unit testing robolectric,没有与“receiveResult”匹配的意图`,unit-testing,android-intent,robolectric,Unit Testing,Android Intent,Robolectric,请参阅我的代码: doSomeActionWillStartNewIntentToSelectImage(); // check started intent Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); Assert.assertThat(activity, new StartedMatcher(intent)); // simulate the r
doSomeActionWillStartNewIntentToSelectImage();
// check started intent
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
Assert.assertThat(activity, new StartedMatcher(intent));
// simulate the returning result
shadowOf(activity).receiveResult(
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI),
Activity.RESULT_OK,
new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));
但它报告了一个错误:
java.lang.RuntimeException: No intent matches
Intent{action=android.intent.action.PICK, data=content://media/internal/images/media} among
[Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}]
at com.xtremelabs.robolectric.shadows.ShadowActivity.receiveResult(ShadowActivity.java:381)
奇怪的是,Intent{action=android.Intent.action.PICK,数据=content://media/internal/images/media}
与后面的代码完全相同
哪里出错以及如何解决?传递到
receiveResult
的意图与活动启动的意图不同
正确的测试代码应为:
doSomeActionWillStartNewIntentToSelectImage();
// check started intent
Intent intent = shadowOf(activity).getNextStartedActivity();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_PICK);
assertThat(intent.getData()).isEqualTo(MediaStore.Images.Media.INTERNAL_CONTENT_URI);
// simulate the returning result
shadowOf(activity).receiveResult(
intent,
Activity.RESULT_OK,
new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));
传递到receiveResult的意图与活动启动的意图不同 正确的测试代码应为:
doSomeActionWillStartNewIntentToSelectImage();
// check started intent
Intent intent = shadowOf(activity).getNextStartedActivity();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_PICK);
assertThat(intent.getData()).isEqualTo(MediaStore.Images.Media.INTERNAL_CONTENT_URI);
// simulate the returning result
shadowOf(activity).receiveResult(
intent,
Activity.RESULT_OK,
new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));