1

查看我的代码:

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, data=content://media/internal/images/media}是和后面的一模一样。

哪里出了问题以及如何解决?

4

1 回答 1

7

传入的意图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)));
于 2012-10-01T07:05:41.077 回答