首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用意式咖啡观景机与吊床匹配器

使用意式咖啡观景机与吊床匹配器
EN

Stack Overflow用户
提问于 2016-02-09 18:03:06
回答 2查看 2.7K关注 0票数 0

有谁能告诉我为什么这个不能用

代码语言:javascript
复制
onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(anyString())));

在logcat中显示了这一点:

代码语言:javascript
复制
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with error: is ""' doesn't match the selected view.
Expected: with error: is ""
Got: "AppCompatEditText{id=2131492985, res-name=edt_apikey, visibility=VISIBLE, width=517, height=83, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x80001 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=1c915e3b5d42d05136185030892fbb846c278927 label=null packageName=null fieldId=0 fieldName=null extras=null ], x=139.0, y=83.0, text=, error-text=This field is required, hint=1c915e3b5d42d05136185030892fbb846c278927, input-type=524289, ime-target=false, has-links=false}"

我想上面这些都不起作用,因为这是一场Mockito的比赛。所以我试了一下:

代码语言:javascript
复制
    import static android.support.test.espresso.matcher.ViewMatchers.hasErrorText;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(not(isEmptyOrNullString()))));

而且也不管用。给予这个例外..。

代码语言:javascript
复制
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
                                                                     at android.support.test.espresso.matcher.ViewMatchers$34.matchesSafely(ViewMatchers.java:1130)
                                                                     at android.support.test.espresso.matcher.ViewMatchers$34.matchesSafely(ViewMatchers.java:1120)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-27 17:07:26

临时解决方案是创建一个自定义匹配程序。

代码语言:javascript
复制
public static Matcher<View> hasErrorText(final Matcher<String> stringMatcher) {
    checkNotNull(stringMatcher);
    return new BoundedMatcher<View, EditText>(EditText.class) {

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

        @Override
        protected boolean matchesSafely(EditText view) {
            if (view.getError() == null) return stringMatcher.matches(view.getError());
            return stringMatcher.matches(view.getError().toString());
        }
    };
}

然后像onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(not(Matchers.isEmptyOrNullString()))));一样使用它

票数 2
EN

Stack Overflow用户

发布于 2016-02-20 20:32:16

看来您的测试代码:

代码语言:javascript
复制
    onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(anyString())));

返回null对象。我没有使用anything() Hamcrest matcher,但是如果您真的想检查错误消息是否为空,那么最好的选择是这样的代码:

代码语言:javascript
复制
     onView(withId(R.id.edt_apikey)).check(matches(withText("")));

希望能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35298998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档