2012-06-18 9 views
7

Estoy emitiendo un intento cuando se hace clic en el botón "grabar". se pasa una variable booleana, que muestra si la grabación se inició o no. El código para generar un intento es:Cómo probar que se ha emitido un intento

Intent recordIntent = new Intent(ACTION_RECORDING_STATUS_CHANGED); 
recordIntent.putExtra(RECORDING_STARTED, getIsRecordingStarted()); 
sendBroadcast(recordIntent); 

Para probar este código, he registrado un receptor en la prueba. La intención se recibe pero la variable aprobada no es la misma. Si depuro el código, puedo ver que el valor es el mismo que se envía, pero cuando lo obtengo, no es el mismo valor.

@Test 
public 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(); 

} 

También he probado en contra de la intención real en lugar de su sombra, pero mismo resultado

Respuesta

3

utilizando el get() en lugar de getBoolean() trabajó para mí.

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(); 

} 
+2

¿Alguna de las afirmaciones de 'BroadcastReceiver' se llama realmente? Intenté tanto 'assertThat (intent.hasExtra (AppActivity.RECORDING_STARTED), equalTo (true));' y 'assertThat (intent.hasExtra (AppActivity.RECORDING_STARTED), equalTo (false));' y mis pruebas no fallaron en ambos casos. Entonces, mi suposición es que esas afirmaciones afirmativas nunca son llamadas. – iRuth

+0

No, no se llama. – zavidovych

0

Esto podría no ayudar a la original, pero, la gente del futuro: si por casualidad te encuentras en esta situación - en primer lugar comprobar sus constantes y filtros intención son distintos de manera que una emisión involuntaria no es recibida por el receptor . Varias veces he pasado más tiempo del que me importa admitir con ese problema.

Cuestiones relacionadas