Skip to content

Instantly share code, notes, and snippets.

@realdadfish
Last active March 26, 2020 11:19
Show Gist options
  • Save realdadfish/5a4f1806d078b6d4d5bd99af1a2b9834 to your computer and use it in GitHub Desktop.
Save realdadfish/5a4f1806d078b6d4d5bd99af1a2b9834 to your computer and use it in GitHub Desktop.
Test Utilities
val singleParamUseCase = ...
// single execution (param is stubbed as "any()")
singleParamUseCase.stubObserver {
onSuccess("yay")
}
viewModel.triggerSomething()
singleParamUseCase.execute("some-param")
// multi execution
singleParamUseCase.stubObserver("correct") {
onSuccess("yay")
}
singleParamUseCase.stubObserver("wrong") {
onError(IllegalStateException())
}
viewModel.triggerSomething()
singleParamUseCase.execute("correct")
singleParamUseCase.execute("wrong")
// Production code
interface Foo {
fun doFoo(): Completable
}
interface Bar {
fun doBar(): Completable
}
fun bla(foo: Foo, bar: Bar): Completable =
foo.doFoo().onErrorComplete().andThen(bar.doBar())
// Test code
val foo = mock<Foo> {
on { doFoo() } doReturn Completable.error(IllegalStateException())
}
val rule = SubscriptionCountsRule()
val bar = mock<Bar> {
on { doBar() } doAnswer { inv ->
rule.ignoredCompletable(inv.identify())
}
}
bla(foo, bar)
.test()
.assertError { it is IllegalStateException }
verify(foo).doFoo()
verify(bar).doBar() // <-- but is it subscribed?!
rule.verifySubscriptionCounts() // <-- now we know!
// Pro Tip: If you include the rule as @Rule into your test,
// verifySubscriptionCounts() is automatically called after your test ran
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment