UnitTests教程(RxTest、swift、RxSwift)

refering to:https://github.com/ReactiveX/RxSwift/blob/master/Documentation/UnitTests.md
UnitTests(Testing custom operators–RxSwift usesRxTest for all operator tests,located in the allTests-*target inside the project Rx.xcworkspace.This is an example of a typical RxSwift operator unite test:)
func testMap_Range() {

 let scheduler = TestScheduler(initialClock: 0)
 let xs = scheduler.createHotObservable([
     next(150, 1),
     next(210,0),
     next(220,1),
     next(230,2),
     next(240,4),
     completed(300)

])

let res = scheduler.start {xs.map {$0 * 2}}
let correctMessage = [
     next(210, 0 * 2),
     next(220,1 * 2),
     next(230,2 * 2),
     next(240,4 * 2),
     completed(300)

]
let correctSubscriptions = [
Subscription(200,300)
]

XCAssertEqual(res.events, correctMessages)
XCAssertEqual(xs.subscriptions, correctSubscriptions)
}

Testing operator compositions (view models,components)
Examples of how to test operator compositions are comtained inside Rx.xcworkspace>RxExample-iOSTests target.
It’s easy to define RxTest extensions so you wcan write your tests in a readable way.Provided example insideRxExample-iOSTests are just suggestions on how you can write those extensions,but there are a lot of possiblities on how to write those tests.
//expected events and test data
let (
usernameEvents,
passwordEvents,
repeatedPasswordEvents,
loginTapEvents,

expectedValidateUsernameEvents,
expectedSignupEnabledEvents
) = (
scheduler.parseEventsAndTimes(“e—-u1—u2——u3”,values:stringValues).first!,
scheduler.parseEventsAndTimes(“e———-p1”,values: stringValues).first!,
scheduler.parseEventsAndTimes(“e——p2—p1-“,values:stringValues ).first!,
scheduler.parseEventsAndTimes(“———”,values.events).first!,

shedulers.parseEventsAndTimes(“e—v—f—v—-f—-v—0——”,values: validations).first!,
scheduler.parseEventsAndTimes(“f————-t–”,values: booleans).first!
)

Integration tests
It is also possible to write integration tests by using RxBlocking operators.
Using RxBlocking’s toBlocking() method,you can block the current thread and wait for the sequence to complete,allowingyou to synchronously access its result.
A simple way to test the result of your sequence is using the toArray method.It will return an array of all elements emitted once a sequence has completed successfully,or throw if an error caused the sequence to terminate.
let result = try fetchResource(location)
.toBlocking()
.toArray()
XCTAssertEqual(result, expecctedResult))

Another option would be to use the materialize operator which lets you more granularly examine your sequence.It will return a MaterializedSequenceResult enumeration that could be either.completed along with the emitted elements if the sequence completed successfully, or failed if the sequence terminated with an error,along with the emitted error.
let result = try fetchResource(location)
.toBlocking()
.materialize()
//for testing the results or error in the case of terminating with error
switch result {
case .completed:
XCTFail(“Expected result to complete with error,but result was successful.”)
case .failed(let elements, let error):
XCTAssertEqual(elements, expectedResult)
XCTAssertErrorEqual(error, expectedError)
}

//for testing the results in the case of termination with completion
switch result {
case .completed(let elements):
XCTAsssertEqual(elements, expectedResult)
case .failed(_, let error):
XCFail(“expected (error)”)
}

你可能感兴趣的:(iOS)