Skip to content

Commit f236f51

Browse files
author
Vinay S Shenoy
committed
Add method to declare expected unsubscribed completable counts in tests
We have tests where `Completable` instances get created, but not subscribed to and this is intentional (for instance, verifying that certain things do *NOT* happen). For these cases, we need some way to indicate to the test rule that we expect a certain test to not subscribe to anything. This commit adds support for this by adding a meta annotation, `ExpectUnsubscribed`, which can be used to annotate a test method.
1 parent 42fb999 commit f236f51

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.simple.clinic.util
2+
3+
@Retention(AnnotationRetention.RUNTIME)
4+
@Target(AnnotationTarget.FUNCTION)
5+
annotation class ExpectUnsubscribed(val completables: Int = 0)

app/src/sharedTest/java/org/simple/clinic/util/RxErrorsRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RxErrorsRule : TestRule {
4040
subscriptionTracker.stopTracking()
4141

4242
assertNoErrors()
43-
subscriptionTracker.assertAllCompletablesSubscribed()
43+
subscriptionTracker.assertAllCompletablesSubscribed(description.getAnnotation(ExpectUnsubscribed::class.java))
4444
}
4545
}
4646
}

app/src/sharedTest/java/org/simple/clinic/util/RxJavaSubscriptionTracker.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ class RxJavaSubscriptionTracker {
2323
RxJavaPlugins.setOnCompletableAssembly(null)
2424
}
2525

26-
fun assertAllCompletablesSubscribed() {
26+
fun assertAllCompletablesSubscribed(
27+
expectUnsubscribed: ExpectUnsubscribed? = null
28+
) {
2729
val assembledCount = assembledCompletables.size
2830
val subscribedCount = assembledCompletables.count { it.hasBeenSubscribedTo }
29-
val haveAllBeenSubscribed = assembledCount == subscribedCount
31+
val expectedUnsubscribedCount = expectUnsubscribed?.completables ?: 0
32+
val totalUnsubscribedCount = assembledCount - subscribedCount
3033

31-
if (!haveAllBeenSubscribed) {
32-
throw AssertionError("Assembled $assembledCount Completables, but only subscribed to $subscribedCount!")
34+
if (totalUnsubscribedCount != expectedUnsubscribedCount) {
35+
throw AssertionError("Assembled $assembledCount Completables, but only subscribed to $subscribedCount, expecting $expectedUnsubscribedCount to not be subscribed")
3336
}
3437
}
3538

0 commit comments

Comments
 (0)