Last active
January 18, 2019 12:33
-
-
Save grumpyshoe/4ae460105bb977ed6e2f9668f82f8c8b to your computer and use it in GitHub Desktop.
Example of mocked Dagger injection for UI-Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MockApiService { | |
// all available methods | |
enum class Method { | |
getData | |
} | |
// expatiations | |
private val expectationMap = mutableMapOf<Method, Expectation>() | |
/** | |
* set expectations | |
* | |
*/ | |
fun setExpectation(method: Method, expectedResult: Expectation) { | |
expectationMap.put(method, expectedResult) | |
} | |
/** | |
* reset all expectation so the default will be used | |
* | |
*/ | |
fun resetExpectation() { | |
expectationMap.clear() | |
} | |
/** | |
* hold repository object | |
* and return mocked data | |
* | |
*/ | |
val service = object : ApiService { | |
override fun getData(onResponse: (response: JSONArray) -> Unit, onError: (error: ANError) -> Unit) { | |
expectationMap.get(Method.getData)?.let { | |
it.success?.let { | |
val mockSuccess = it.invoke() | |
onResponse(mockSuccess.result as JSONArray) | |
return | |
} | |
it.failure?.let { | |
val mockFailure = it.invoke() | |
onError(ANError(mockFailure.error)) | |
return | |
} | |
} | |
val myArray = JSONArray() | |
myArray.put("test") | |
return onResponse(myArray) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment