Last active
June 26, 2016 10:49
-
-
Save AAverin/5679c598ec7f32bd1e8ca45540ae1478 to your computer and use it in GitHub Desktop.
gist for CleanAndroidCode article series https://medium.com/@anton.averin.dev/keep-your-droid-clean-e9c093140eb6
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
@RunWith(PowerMockRunner::class) | |
@PrepareForTest(RxBus::class, CompositeSubscription::class, Observable::class) | |
class BusSubscriberTest { | |
@Mock lateinit var subscription: Subscription | |
@Mock lateinit var bus: RxBus | |
@Mock lateinit var observable: Observable<Any> | |
@Captor var subscriptionCaptor = ArgumentCaptor.forClass(Subscription::class.java) | |
@InjectMocks lateinit var classToTest: BusSubscriber | |
@Before | |
fun setup() { | |
classToTest.bus(bus) | |
} | |
@Test | |
fun subscribingAddsToSubscriptionsList() { | |
// given | |
val clazz = Any::class | |
given(bus.events(clazz.java)).willReturn(observable) | |
given(observable.subscribe(any<Action1<Any>>())).willReturn(subscription) | |
// when | |
classToTest.subscribe(clazz, {}) | |
// then | |
assertThat(classToTest.subscriptions.get(0), isEqualTo(subscription)) | |
} | |
@Test | |
fun subscribingSubscribesToBusEvents() { | |
// given | |
given(bus.events(Any::class.java)).willReturn(Observable.empty()) | |
// when | |
classToTest.subscribe(Any::class, {}) | |
// then | |
verify(bus).events(Any::class.java) | |
} | |
@Test | |
fun unsubscribingUnsubscribesAndClearsSubscriptionsList() { | |
// given | |
classToTest.subscriptions.add(subscription) | |
// when | |
classToTest.unsubscribe() | |
// then | |
verify(subscription).unsubscribe() | |
assertThat(classToTest.subscriptions.size, isEqualTo(0)) | |
} | |
} |
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
@RunWith(PowerMockRunner::class) | |
class RxBusTest { | |
val classToTest = RxBus() | |
@Test | |
fun postingEventOnBusAppearsInEvents() { | |
// given | |
var testEvent = TestEvent() | |
var resultEvent: Any? = null | |
classToTest.events().subscribe { | |
resultEvent = it | |
} | |
// when | |
classToTest.post(testEvent) | |
// then | |
Assert.assertThat(resultEvent as? TestEvent, CoreMatchers.`is`(testEvent)) | |
} | |
@Test | |
fun postingEventOnBusAppearsInEventsForType() { | |
// given | |
var testEvent = TestEvent() | |
var resultEvent: Any? = null | |
classToTest.events(TestEvent::class.java).subscribe { | |
resultEvent = it | |
} | |
// when | |
classToTest.post(testEvent) | |
// then | |
Assert.assertThat(resultEvent as? TestEvent, CoreMatchers.`is`(testEvent)) | |
} | |
class TestEvent : BusEvent | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment