Skip to content

Instantly share code, notes, and snippets.

@Raiden18
Last active July 13, 2022 08:49
Show Gist options
  • Save Raiden18/8e5d6748bf08dcfee1b38714e2236642 to your computer and use it in GitHub Desktop.
Save Raiden18/8e5d6748bf08dcfee1b38714e2236642 to your computer and use it in GitHub Desktop.
private inner class ViewModelBuilder {
fun withLoadingData(): ViewModelBuilder {
// For RxJava it would be every { someInteractor.loadSomething() } returns Single.never()
coEvery { someInteractor.loadSomething() } coAnswers {
delay(1_000)
"result is not important"
}
return this
}
fun withDataLoadedWithError(throwable: Throwable): ViewModelBuilder {
coEvery { someInteractor.loadSomething() } coAnswers { throw throwable }
return this
}
fun withLoadedData(dataToLoad: String): ViewModelBuilder {
coEvery { someInteractor.loadSomething() } returns dataToLoad
return this
}
fun build(testScheduler: TestCoroutineScheduler): SomeViewModel {
val dispatechers = TestDispatchersProvider(testScheduler)
val viewModel = SomeViewModel(someInteractor, dispatechers)
return viewModel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment