Skip to content

Instantly share code, notes, and snippets.

@grumpyshoe
Last active January 18, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grumpyshoe/4ae460105bb977ed6e2f9668f82f8c8b to your computer and use it in GitHub Desktop.
Save grumpyshoe/4ae460105bb977ed6e2f9668f82f8c8b to your computer and use it in GitHub Desktop.
Example of mocked Dagger injection for UI-Test
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