简体   繁体   English

如何测试 lambda 从 Kotlin 中的 function 返回

[英]How can I test that a lambda is returned from a function in Kotlin

I have a function that updates a ViewModel Livedata with an Event which contains a lambda.我有一个 function,它使用包含 lambda 的事件更新 ViewModel Livedata。 () -> Unit and I want to test that my lambda is returned in my LiveData. () -> Unit ,我想测试我的 lambda 是否在我的 LiveData 中返回。 With an object was easily done with an Assert.equals but now with a lambda I have no idea how to do it.使用 object 很容易使用 Assert.equals 完成,但现在使用 lambda 我不知道该怎么做。

Here's what I got until now.这是我到目前为止所得到的。

fun retrieveData() : {
  viewModelScope.launch{
   val myData = usecase.retrieveData()
   if (myData != null) myDataLiveData.value = myData
   else errorLiveData.value = Event { return@MyViewModel.retrieveData() }
  }
}

In my test I have:在我的测试中,我有:

    subject.errorLiveData.observeForTesting {
        assert(subject.errorLiveData.value!!.peekContent() != null) // This "works" but shows a hint that the comparison is always true even if I try it with == null and the assertion fails
    }

Here's also the Event class.这也是事件 class。

/**
 * Used as a wrapper for data that is exposed via a LiveData that represents an event.
 */
open class Event<out T>(private val content: T) {

    var hasBeenHandled = false
        private set // Allow external read but not write

    /**
     * Returns the content and prevents its use again.
     */
    fun getContentIfNotHandled(): T? {
        return if (hasBeenHandled) {
            null
        } else {
            hasBeenHandled = true
            content
        }
    }

    /**
     * Returns the content, even if it's already been handled.
     */
    fun peekContent(): T = content
}

I take it from here: https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150我从这里拿它:https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150

Thanks谢谢

You can use lambda function as below in kotlin...您可以在 kotlin...

subject.errorLiveData?.apply{
   assert(this.value.peekContent())
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 kotlin 中测试 lambda 函数 - How to test lambda function in kotlin 如何在 kotlin 中使用 find 扩展函数并在 lambda 中使用它的索引 - How can I use find extention function in kotlin and have it's index in the lambda 如何访问从 Cloud Function(kotlin/typescript)返回给客户端的数组元素? - How do I access the elements of the array returned to the client from a Cloud Function (kotlin/typescript)? 如何查看从 kotlin 上的 rest api 返回的验证错误? - How can I see the validation error returned from the rest api on kotlin? 使用Kotlin扩展函数的Lambda时,如何限制从内部Lambda访问外部Lambda方法? - How to restrict access to the outer Lambda method from the inner Lambda when using the Lambda of the Kotlin extension function? Kotlin:如何从 function 访问 LayoutInflater? - Kotlin: How can i access the the LayoutInflater from a function? Kotlin:如何将值从 lambda 返回到父 function? - Kotlin: How to return a value from a lambda to a parent function? 我可以从 Dart 调用 Kotlin 函数吗 - Can I call Kotlin function from Dart Kotlin如何调用扩展功能 - Kotlin How can I call extension function 为什么我不能在 lambda 函数下调用 kotlin 挂起函数 - Why can't I call kotlin suspend function under lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM