tcldr / Entwine

Testing tools and utilities for Apple's Combine framework.
MIT License
445 stars 26 forks source link

Add toBlocking operator like RxSwift #4

Open bitomule opened 5 years ago

bitomule commented 5 years ago

RxSwift includes and useful operator that transform any observable into a blocking sequence.

For Combine it'll be really useful being able to implement the same behavior. Here's how it works on RxSwift http://rx-marin.com/post/rxblocking-part1/

I got something working using current code but it's far from ready, it may help us getting the idea of the expected behavior:

let testScheduler = TestScheduler()

let publisher = TestPublisher<Int, TestError>.init { subscriber in
    subscriber.receive(1)
    subscriber.receive(2)
}

let configuration = TestScheduler.Configuration(pausedOnStart: false, created: 0, subscribed: 0, cancelled: 1, subscriberOptions: TestableSubscriberOptions(initialDemand: Subscribers.Demand.unlimited, subsequentDemand: Subscribers.Demand.unlimited, demandReplenishmentDelay: 0, negativeBalanceHandler: { }))

let testableSubscriber = testScheduler.start(configuration: configuration) { publisher }

XCTAssertEqual(testableSubscriber.sequence, [
    (0, .input(1)),
    (0, .input(2)),
    (0, .subscription),
])

The expected behavior would be:


let publisher = TestPublisher<Int, TestError>.init { subscriber in
    subscriber.receive(1)
    subscriber.receive(2)
}

let blockingPublisher = publisher.toBlocking()

XCTAssertEqual(blockingPublisher.sequence, [1, 2])

This will allow testing Combine publishers in libraries like https://github.com/bitomule/CombineRealm

Here's the RxSwift implementation: https://github.com/ReactiveX/RxSwift/tree/master/RxBlocking

Sajjon commented 4 years ago

@bitomule @tcldr FYI https://github.com/groue/CombineExpectations