android - Espresso 自定义 ViewMatcher 不匹配描述未出现在日志中

标签 android unit-testing automated-tests android-espresso

我为自定义 View 编写了以下 View 匹配器

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

当匹配失败时,日志不包含我在 descibeMismatch() 函数中附加的不匹配描述。有什么我错过的吗?

最佳答案

我遇到了同样的问题。直到 feature request实现后,您可以使用包含不匹配原因的自定义 ViewAssertion:

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

像这样使用它:

onView(...).check(EspressoUtils.matches(...))

关于android - Espresso 自定义 ViewMatcher 不匹配描述未出现在日志中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568080/

相关文章:

node.js - 使用依赖注入(inject)在 Typescript 中进行单元测试

python - Selenium /Python : How do I move to a new window with no window name given?

TFS '$(BuildLocation)' 宏指向实验室环境模板上的错误路径

"onActivityResult"处的 Android 变量有时在 KITKAT 上为空值

java - 使用 php 和 MySql 在 android 中检查复选框

unit-testing - 如何让 maven 在激活非默认配置文件的情况下运行单个测试类?

unit-testing - 如何使用 TDD/BDD 开发输入对象?

java - 无法使用 Selenium webdriver 切换到 iframe

android - 如何从android webview将打印推送到蓝牙热敏打印机?

android - 为什么在 android 中单击我的应用程序的后退按钮后,我以前的 Activity 会自动刷新?