Rx Behavior Testing by stream pattern matching and assertions
struct State {
var isLoading: Bool
var isSaveEnabled: Bool
var name: String
}
let observable: Observable<State> = ...
let decision = testBehavior(of: observable) { dsl in
dsl.match { $0.isLoading == false }
dsl.assert(.always) { $0.isLoading == false }
dsl.match { $0.name == "Updated Name" }
dsl.match { $0.isSaveEnabled == true }
dsl.run(.async) { triggerSaving() }
dsl.match { $0.isSaveEnabled == false }
}
// Decision will be
// .correct: If no assertions failed and all match clauses matched
// .failed: If any assetions failed or observable completes or times out without matching dsl.match { state in
predicate
} dsl.assert(.always) { state in
assertion
}Assertion is checked from the point of definition until the test completes
dsl.assert(.untilNextMatch) { state in
assertion
}Only evaluates the assertion until the next matcher after that the assertion is not checked
dsl.unordered {
dsl... // matchers/assertions or other scopes
}Completes when all of the matchers return a result in any order
dsl.first {
dsl... // matchers/assertions or other scopes
}Completes when any of the matchers or assertions returns a result (.correct or .failed)
- Swift 4.2
- RxSwift 4
RxBehaviorTester is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RxBehaviorTester'Eliran Ben-Ezra, eliran@threeplay.com
RxBehaviorTester is available under the MIT license. See the LICENSE file for more info.