Espresso Assertion失败,原因错误

时间:2018-02-10 11:15:19

标签: unit-testing android-testing android-espresso

我一直在尝试在espresso上运行测试,它将为editText提供输入并点击一个按钮然后进行验证,我已经包含依赖尝试清除缓存但仍然面临此错误

这是测试

@RunWith(AndroidJUnit4.class)
public class Espresso {
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void lenCheck() {
        String text = "short";
        onView(withId(R.id.editText )).perform(typeText(text), closeSoftKeyboard());
        onView(withId(R.id.button)).perform(click()) ;
        onView(withId(R.id.textView)).check(matches(withText("Min 8 characters.")));
           }}

给了我以下错误

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Min 8 characters."' doesn't match the selected view.
Expected: with text: is "Min 8 characters."
Got: "AppCompatTextView{id=2131165303, res-name=textView, visibility=VISIBLE, width=0, height=53, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@e24ffa2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=602.0, y=1359.0, text=, input-type=0, ime-target=false, has-links=false}"

这与android studio或框架有关吗?

1 个答案:

答案 0 :(得分:0)

根据Espresso documentation,以下检查应该有效:

onView(withId(R.id.textView)).check(matches(hasErrorText("Min 8 characters.")));