尝试断言列表中的文本视图数量时断言失败

Assertion Failure while trying to assert amount of textviews in a list

当我尝试断言列表中的文本视图数量时,为什么我的断言失败了?

@Test
public void testDeleteNote() throws Exception {

   int count= getNoOfTextViews();
    // Checking if count is greater than 0.
    if (count > 0) {
        // Iterating count times
        for (int i = 0; i < count; i++)
        {
            // Checking if count is even or odd.
            if (i % 2 == 0) {
                solo.clickInList(0);
                deleteNote();
            } else {
                // Clicking Long on the 1st item in the Notes List.
                solo.clickLongInList(0);
                // Clicking on Delete String.
                solo.clickOnText(solo.getString(R.string.menu_delete));
                }
    }

    count = getNoOfTextViews();
    // Asserting all the text views are deleted.
    assertEquals(0, count);
}

  public int getNoOfTextViews() {
    // Getting the count of text views in the activity.
    ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
            solo.getCurrentViews(ListView.class).get(0));
    return textView.size();
}

我看到的失败是:

junit.framework.AssertionFailedError: expected:<0> but was:<1>

更新: 我看到这在我调试时通过了,只有当我 运行 测试用例时它才会失败。

您的 count 变量在您的程序开始时计算一次,然后永远不会更新。这意味着,当您将它与最后的 0 进行比较时,它仍然包含旧值。

您所要做的就是再次调用该方法来更新您的计数变量:

count = getNoOfTextViews();

或者干脆

assertEquals(0, getNoOfTextViews());

在进行计数之前添加了等待列表视图。这帮助我解决了问题。

谢谢大家。

public int getNoOfTextViews() {
    solo.waitForView(ListView.class,0,1000);
    // Getting the count of text views in the activity.
    ArrayList<TextView> textView = solo.getCurrentViews(TextView.class,
            solo.getCurrentViews(ListView.class).get(0));
    return textView.size();
}