dimanche 26 juin 2016

Kotlin Anonymous Function Parameter Unit Testing

As per Kotlin Unit Testing for Function Parameter and Object, we could test the function variable funcParam, as it is an object function variable.

However if code is written using anonymous/inlining function parameter (which is a very nice Kotlin feature, that allow us to eliminate unnecessary temp variable for it)...

class MyClass1(val myObject: MyObject) {
    fun myFunctionOne() {
        myObject.functionWithFuncParam{ 
            num: Int ->
            // Do something to be tested
            println(num)
        }
    }
}

class MyObject () {
    fun functionWithFuncParam(funcParam: (Int) -> Unit) {
        funcParam(32)
    }
}

How to write my unit test that test this part of code?

            num: Int ->
            // Do something to be tested
            println(num)

Or the inlining of the function parameter (as above) is something not good for unit testing, and hence should be avoided

Aucun commentaire:

Enregistrer un commentaire