Intent recordIntent = new Intent(ACTION_RECORDING_STATUS_CHANGED);recordIntent.putExtra(RECORDING_STARTED,getIsRecordingStarted());sendbroadcast(recordIntent);
为了测试这段代码,我在测试中注册了一个接收器.收到意图但传递的变量不一样.如果我调试代码,我可以看到该值与发送时相同,但是当我得到它时,它的值不一样.
@Testpublic voID pressingRecordbuttonOnceGenerateStartRecordingIntent() throws Exception { // Assign AppActivity activity = new AppActivity(); activity.onCreate(null); activity.onResume(); activity.registerReceiver(new broadcastReceiver() { @OverrIDe public voID onReceive(Context arg0,Intent intent) { // Assert ShadowIntent shadowIntent = Robolectric.shadowOf(intent); assertthat(shadowIntent .hasExtra(AppActivity.RECORDING_STARTED),equalTo(true)); Boolean expected = true; Boolean actual = shadowIntent.getExtras().getBoolean( AppActivity.RECORDING_STARTED,false); assertthat(actual,equalTo(expected)); } },new IntentFilter( AppActivity.ACTION_RECORDING_STATUS_CHANGED)); Imagebutton recordbutton = (Imagebutton) activity .findVIEwByID(R.ID.recordBtn); // Act recordbutton.performClick(); ShadowHandler.IDleMainLooper();}
我也测试了实际的意图,而不是它的阴影,但结果相同
解决方法 使用get()而不是getBoolean()为我工作.public voID pressingRecordbuttonOnceGenerateStartRecordingIntent() throws Exception { // Assign BreathAnalyzerAppActivity activity = new AppActivity(); activity.onCreate(null); activity.onResume(); activity.registerReceiver(new broadcastReceiver() { @OverrIDe public voID onReceive(Context arg0,Intent intent) { // Assert assertthat(intent .hasExtra(AppActivity.RECORDING_STARTED),equalTo(true)); Boolean expected = true; Boolean actual = (Boolean)intent.getExtras().get( AppActivity.RECORDING_STARTED); assertthat(actual,equalTo(expected)); } },new IntentFilter( AppActivity.ACTION_RECORDING_STATUS_CHANGED)); Imagebutton recordbutton = (Imagebutton) activity .findVIEwByID(R.ID.recordBtn); // Act recordbutton.performClick(); ShadowHandler.IDleMainLooper();}总结
以上是内存溢出为你收集整理的android – 如何测试已广播的意图全部内容,希望文章能够帮你解决android – 如何测试已广播的意图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)