Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Created October 14, 2020 22:47
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 AfzalivE/c4201529a7fe4e8e937d3f6c690e208d to your computer and use it in GitHub Desktop.
Save AfzalivE/c4201529a7fe4e8e937d3f6c690e208d to your computer and use it in GitHub Desktop.
fake() for all the unit tests
val ALL_FAKES = listOf(
FakePostLoginUseCase::class,
// all fake classes go here
)
/**
* Finds and initializes an instance of
* class [T] if it exists in [ALL_FAKES].
*
* If it doesn't, a [FakeNotFoundException] is thrown
*/
inline fun <reified T : Any> fake(): T {
val match = ALL_FAKES.firstOrNull {
it.isSubclassOf(T::class)
}
match ?: throw fakeNotFoundException<T>()
return match.createInstance() as T
}
inline fun <reified T : Any> fakeNotFoundException() = FakeNotFoundException(T::class.simpleName)
class FakeNotFoundException(kClass: String?) : Throwable("No matching fakes were found for type: $kClass")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment