1

我正在尝试为in编写Robolectric测试用例,但无论文本的长度是多少,我总是得到一个。 这是我正在编写的测试用例:line countTextView1line count

@Test
  fun test() {
    launch(TopicInfoTestActivity::class.java).use {
      it.onActivity { activity ->
        val descriptionTextView = getTopicDescriptionTextView(activity)
        assertThat(descriptionTextView?.lineCount).isEqualTo(5)
      }
    }
  }

测试结果:

expected: 5
but was : 1

另一个测试用例:

@Test
  fun secondTest() {
    launchTopicActivityIntent().use {
      onView(withId(R.id.topic_description_text_view))
        .check(matches(lineCount(5)))
    }
  }

这里我使用了一个自定义匹配器,这是它的代码:

private fun lineCount(lineCount: Int): TypeSafeMatcher<View> {
    return object : TypeSafeMatcher<View>() {
      var count = 0
      override fun matchesSafely(item: View): Boolean {
        count = (item as TextView).lineCount
        return item.lineCount == lineCount
      }

      override fun describeTo(description: Description) {
        description.appendText("isTextInLines: $lineCount Got: $count")
      }
    }
  }

测试结果:

Expected: isTextInLines: 5 Got: 1

知道如何用 测试行数Robolectric吗?

4

0 回答 0