vendredi 27 septembre 2019

Kotlin: Test lamda expression callback

I'm trying to write unit for the following rest api call, my method looks like this

fun authenticate(email: String,
                 password: String,
                 onSuccess: () -> Unit,
                 onError: (message: String) -> Unit): Disposable? {

    val mapper = AuthMapper(email, password)
    val request = RestRequest(AuthRestRequestErrorHandler(), this.restService.authenticate(mapper))


    return  request.handleObservable<AuthResponseMapper>(
        onSuccess = { authResponseMapper ->
            this.storeAuthSession(LoginProvider.regular, authResponseMapper)
            onSuccess()
        },
        onError = { error ->
            onError(error.message)
        }
    )
}

The goal is call authentication endpoint, receive auhtorization token and test one more similar method which can be called only with authorization token.

I'm coming from iOS where you have waitForExpectations and expectation.fulfill() methods which help me to wait for api response and check result. So is there anything similar on android or any other suggestions or correct approaches how to do that?

Aucun commentaire:

Enregistrer un commentaire