Skip to content

Instantly share code, notes, and snippets.

@louis993546
Created May 16, 2019 07:24
Show Gist options
  • Save louis993546/ad112ed89dcb655429a54869c56a30d8 to your computer and use it in GitHub Desktop.
Save louis993546/ad112ed89dcb655429a54869c56a30d8 to your computer and use it in GitHub Desktop.
class PresenterWithDI(
private val networking: NetworkWrapper,
private val crashReporting: CrashReporting
) {
fun onSaveClicked() {
val (response, error) = networking.callTheApi()
if (error != null)
crashReporting.log("Oh no")
else
Log.d("DI", "Yay")
}
}
class PresenterWithDITest {
private val mockNetworking = mock<NetworkWrapper>()
private val mockCrashReporting = mock<CrashReporting>()
private var presenter = PresenterWithDI(mockNetworking, mockCrashReporting)
@Test
fun networkError_onSaveClicked_logCrash() {
//given
when(mockNetworking.callTheApi()).thenReturn(null to ApiWentDownException("pretent API wen't down"))
//when
presenter.onSaveClicked()
//then
verify(mockCrashReporting).log("Oh no")
}
}
class PresenterWithoutDI {
private val networking: NetworkWrapper = NetworkWrapperImpl()
private val crashReporting: CrashReporting = CrashReporting.getInstance()
fun onSaveClicked() {
val (response, error) = networking.callTheApi()
if (error != null)
crashReporting.log("Oh no")
else
Log.d("DI", "Yay")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment