Espresso - not(isDisplayed())vs doesNotExist()之间的区别

时间:2017-09-10 13:14:55

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

当我遇到这两个测试时,我正在阅读Android架构蓝图中的todoapp。我似乎无法理解为什么有必要在doesNotExist()中使用createTwoTasks_deleteOneTask()来检查具有指定标题的项目是否已删除,何时可以在not(isDisplayed())中使用createOneTask_deleteTask() {1}}检查同样的事情?

@Test
public void createOneTask_deleteTask() {
    viewAllTasks();

    // Add active task
    createTask(TITLE1, DESCRIPTION);

    // Open it in details view
    onView(withText(TITLE1)).perform(click());

    // Click delete task in menu
    onView(withId(R.id.menu_delete)).perform(click());

    // Verify it was deleted
    viewAllTasks();
    onView(withText(TITLE1)).check(matches(not(isDisplayed())));
}

@Test
public void createTwoTasks_deleteOneTask() {
    // Add 2 active tasks
    createTask(TITLE1, DESCRIPTION);
    createTask(TITLE2, DESCRIPTION);

    // Open the second task in details view
    onView(withText(TITLE2)).perform(click());

    // Click delete task in menu
    onView(withId(R.id.menu_delete)).perform(click());

    // Verify only one task was deleted
    viewAllTasks();
    onView(withText(TITLE1)).check(matches(isDisplayed()));
    onView(withText(TITLE2)).check(doesNotExist());
}

我是Espresso测试的新手,但我知道通常会调用doesNotExist()来确认视图已经过去,但是为什么not(isDisplayed())即使在删除视图时也能正常工作?我尝试使用doesNotExist(),但它会抛出错误。

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: View is present in the hierarchy: AppCompatTextView{id=2131624010, res-name=title, visibility=VISIBLE, width=1120, height=107, 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, root-is-layout-requested=false, has-input-connection=false, x=256.0, y=74.0, text=TITLE1, input-type=0, ime-target=false, has-links=false}
Expected: is <false>
Got: <true>

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at com.example.android.architecture.blueprints.todoapp.tasks.TasksScreenTest.createOneTask_deleteTask(TasksScreenTest.java:284)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1959)
Caused by: junit.framework.AssertionFailedError: View is present in the hierarchy: AppCompatTextView{id=2131624010, res-name=title, visibility=VISIBLE, width=1120, height=107, 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, root-is-layout-requested=false, has-input-connection=false, x=256.0, y=74.0, text=TITLE1, input-type=0, ime-target=false, has-links=false}
Expected: is <false>
Got: <true>

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$1.check(ViewAssertions.java:62)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 个答案:

没有答案