2015-09-14 6 views
10

ho scritto il seguente vista matcher per la mia vista personalizzatadescrizione Espresso personalizzato ViewMatcher Mancata corrispondenza che non appare nel log

public static Matcher<View> withValue(final Matcher<Long> longMatcher){ 
    return new BoundedMatcher<View, IntegerField>(IntegerField.class) { 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("with value : "); 
      longMatcher.describeTo(description); 
     } 

     @Override 
     public void describeMismatch(Object item, Description description) { 
      super.describeMismatch(item, description); 
      description.appendText("value=" + ((IntegerField)item).getValue()); 
     } 

     @Override 
     protected boolean matchesSafely(IntegerField field) { 
      return longMatcher.matches(field.getValue()); 
     } 
    }; 

quando la partita non riesce, il registro non contiene la descrizione non corrispondente I allegata al descibeMismatch() funzione. C'è qualcosa che mi è mancato?

+1

hanno lo stesso problema ... qualche pista? –

+3

Non ho una soluzione alternativa, ma almeno ho trovato la causa principale: il metodo ViewAssertions.matches() non richiama discribeMismatch poiché chiama assertThat (message, actual, matcher) invece di assertThat (actual, matcher). –

+2

Ho incontrato più volte e alla fine ho registrato una richiesta di funzionalità per ottenerlo. Si prega di star per ottenere un po 'di attenzione su di esso: https://code.google.com/p/android/issues/detail?id=234801 – dominicoder

risposta

0

Ho avuto lo stesso problema. Fino a quando il feature request è implementata, è possibile utilizzare un costume ViewAssertion che comprende la ragione per mancata corrispondenza:

public class EspressoUtils { 
    // this class is copied from Espresso's source code 
    // (we need to copy it so that we can replace the `assertThat` function it depends on 
    private final static class MatchesViewAssertion implements ViewAssertion { 
     final Matcher<? super View> viewMatcher; 

     private MatchesViewAssertion(final Matcher<? super View> viewMatcher) { 
      this.viewMatcher = viewMatcher; 
     } 

     public void check(View view, NoMatchingViewException noViewException) { 
      StringDescription description = new StringDescription(); 
      description.appendText("'"); 
      viewMatcher.describeTo(description); 
      if (noViewException != null) { 
       description.appendText(
         String.format(
           "' check could not be performed because view '%s' was not found.\n", 
           noViewException.getViewMatcherDescription())); 
       throw noViewException; 
      } else { 
       description.appendText("' doesn't match the selected view."); 
       assertThat(description.toString(), view, viewMatcher); 
      } 
     } 

     /** 
     * A replacement for ViewMatchers.assertThat that includes the mismatch description (adapted from the source of ViewMatchers.assertThat 
     */ 
     private static <T> void assertThat(String message, T actual, Matcher<T> matcher) { 
      if (!matcher.matches(actual)) { 
       final StringDescription mismatch = new StringDescription(); 
       matcher.describeMismatch(actual, mismatch); 

       Description description = new StringDescription(); 
       description.appendText(message) 
         .appendText("\nExpected: ") 
         .appendDescriptionOf(matcher); 

       if(!mismatch.toString().trim().isEmpty()) { 
        description.appendText("\n But: ").appendText(mismatch.toString()); 
       } 

       description.appendText("\n Got: "); 
       if (actual instanceof View) { 
        description.appendValue(HumanReadables.describe((View) actual)); 
       } else { 
        description.appendValue(actual); 
       } 
       description.appendText("\n"); 
       throw new AssertionFailedError(description.toString()); 
      } 
     } 
    } 

    public static ViewAssertion matches(final Matcher<View> matcher) { 
     return new MatchesViewAssertion(matcher); 
    } 
} 

usare in questo modo:

onView(...).check(EspressoUtils.matches(...)) 
Problemi correlati