【问题标题】:When unit testing my ViewModel, list always returns empty对我的 ViewModel 进行单元测试时,列表总是返回空
【发布时间】:2019-09-09 23:59:22
【问题描述】:

我正在尝试学习单元测试,我将我的代码分解为 MVVM(i) 架构,但是当我运行我的 testParseToList() 测试函数时,它总是返回一个空列表,我想不通出为什么。我担心它可能与 MVVM(i) 的 i 部分有关,以及我是否正确地模拟了我的视图模型。我从最简单的视图模型开始,希望能在了解更复杂的概念之前掌握这些概念。

OfflineViewModelUnitTest.kt

@RunWith(JUnit4::class)
class OfflineViewModelUnitTest {

    @get:Rule
    val rule = InstantTaskExecutorRule()

    @Mock
    var offlineViewModel: OfflineViewModel = OfflineViewModel(OfflineInteractorImpl())

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        DaggerOfflineViewModelComponent.builder()
            .offlineInteractorImplModule(OfflineInteractorImplModule())
            .build()
            .inject(offlineViewModel)

//        this.offlineViewModel = OfflineViewModel(OfflineInteractorImpl())
    }

    @Test
    fun testParseToList() {

        val test = offlineViewModel.parseTextToList("dried bonito extract,\n" +
                "    ketchup,\n" +
                "    millet,\n" +
                "    corn & wheat protein")

        val a = "dried bonito extract"
        val b = "ketchup"
        val c = "millet"
        val d = "corn & wheat protein"

        val expectedResult = listOf(a, b, c, d)

        assertEquals(expectedResult, test)
    }
}

OfflineViewModel.kt

class OfflineViewModel(private val offlineInteractor: OfflineInteractor): ViewModel() {

    init {
        DaggerOfflineViewModelComponent.builder()
            .offlineInteractorImplModule(OfflineInteractorImplModule())
            .build()
            .inject(this)
    }
    fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {
        Log.d("here it is", firebaseVisionTextString)
        return offlineInteractor.parseTextToList(firebaseVisionTextString)
    }

    fun readCsvFromAssetFolder(inputStream: InputStream): List<String>{
        return offlineInteractor.readCsvFromAssetFolder(inputStream)
    }
}

OfflineInteractorImpl.kt

class OfflineInteractorImpl: OfflineInteractor {

    override fun parseTextToList(firebaseVisionTextString: String): MutableList<String> {

            val ingredientsList: MutableList<String> = firebaseVisionTextString.split(",").map { it.trim() }.toMutableList()

            return ingredientsList
    }

    override fun readCsvFromAssetFolder(inputStream: InputStream): List<String> {
        val csvLine: ArrayList<String> = ArrayList()
        var content: Array<String>?
        try
        {
            val br = BufferedReader(InputStreamReader(inputStream))
            for (line in br.lines())
            {
                content = line.split((",").toRegex()).dropLastWhile{ it.isEmpty() }.toTypedArray()
                csvLine.add(content[0].substringBefore(";"))
            }
            br.close()
        }
        catch (e: IOException) {
            e.printStackTrace()
        }
        return csvLine
    }
}

测试结果

java.lang.AssertionError: 
Expected :[dried bonito extract, ketchup, millet, corn & wheat protein]
Actual   :[]

【问题讨论】:

  • offlineViewModel 是您正在测试的课程,但您嘲笑了它。你不应该嘲笑被测类。此外,如果您创建一个模拟,则需要为其定义行为,否则它将仅返回 null(或原始等效项)。

标签: unit-testing kotlin junit mockito


【解决方案1】:

就像第二个说的那样,因为你模拟了offlineViewModel,它会返回一个空字符串,除非你定义了一些东西让它使用when()返回。

来源:https://github.com/mockito/mockito/wiki/FAQ#what-values-do-mocks-return-by-default

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多