Last active
March 26, 2020 11:19
-
-
Save realdadfish/5a4f1806d078b6d4d5bd99af1a2b9834 to your computer and use it in GitHub Desktop.
Test Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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