Welcome to the documentation for RxBlocking, a framework for writing unit tests for code using RxSwift. RxBlocking provides tools to create test observables and operators to block until they emit a value, allowing for synchronous testing using RxSwift.
Installation
To use RxBlocking, you will need to install both RxSwift and RxTest. RxSwift provides the core functionality for reactive programming, while RxTest provides utilities for creating test observables. You can install them using Cocoapods or Swift Package Manager.
“`
pod ‘RxBlocking’, ‘~> 6.3’
pod ‘RxSwift’, ‘~> 6.2’
pod ‘RxCocoa’, ‘~> 6.2’
“`
Getting Started
To get started with RxBlocking, import the necessary modules:
“`
import RxBlocking
import RxSwift
import RxCocoa
import RxTest
“`
Creating Test Observables
RxBlocking provides various operators to create test observables. Let’s explore some of them:
`just()` Operator
The `just(_:)` operator creates a test observable that emits a single element and completes. Here’s an example:
“`
let observable = Observable.just(“Hello, world!”)
“`
`from()` Operator
The `from(_:)` operator creates a test observable from an array of elements. The elements are emitted one by one in the order of the array. Here’s an example:
“`
let observable = Observable.from([1, 2, 3, 4, 5])
“`
Blocking Operators
RxBlocking provides blocking operators that allow you to block until a test observable emits a value. Let’s explore some of them:
`toBlocking()` Operator
The `toBlocking()` operator converts a test observable into a `BlockingObservable`. It allows blocking until the test observable has completed or emitted a value. Here’s an example:
“`
let result = try! observable.toBlocking().single()
“`
`first()` Operator
The `first()` operator blocks until the test observable emits the first element, and then completes. Here’s an example:
“`
let firstElement = try! observable.toBlocking().first()
“`
Additional Resources
For more information and detailed examples, refer to the official RxBlocking documentation: