mardi 6 mars 2018

How to mock Kotlin Object in android?

I have an object in kotlin that controls the current user's session information. I want to mock the sign in method which has a call back.

While testing, I need to mock this method in the SessionController object.

object SessionController {

...

    fun signIn(username: String, password: String, signInCallBack: SignInCallBack) {
        sessionApi.attemptSignIn(username,password,object: SignInCallBack{
            override fun onSignInComplete() {
                signInCallBack.onSignInComplete()
            }

            override fun onErrorOccurred(errorCode: Int, errorMessage: String) {
                signInCallBack.onErrorOccurred(errorCode)
            }

        })
    }
    ....
}

Any suggestions/ideas as to how I can achieve this? I've read online to use Mockk for kotlin but have't been able to mock this method and invoke the appropriate callback. Any suggestions on improving the structure would also be appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire