Guest User

Untitled

a guest
Jan 18th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. Unit Tests
  2. ==========
  3.  
  4. ## Testing custom operators
  5.  
  6. RxSwift uses `RxTests` for all operator tests, located in the AllTests-* target inside the project `Rx.xcworkspace`.
  7.  
  8. This is an example of a typical `RxSwift` operator unit test:
  9.  
  10. ```swift
  11. func testMap_Range() {
  12. // Initializes test scheduler.
  13. // Test scheduler implements virtual time that is
  14. // detached from local machine clock.
  15. // This enables running the simulation as fast as possible
  16. // and proving that all events have been handled.
  17. let scheduler = TestScheduler(initialClock: 0)
  18.  
  19. // Creates a mock hot observable sequence.
  20. // The sequence will emit events at designated
  21. // times, no matter if there are observers subscribed or not.
  22. // (that's what hot means).
  23. // This observable sequence will also record all subscriptions
  24. // made during its lifetime (`subscriptions` property).
  25. let xs = scheduler.createHotObservable([
  26. next(150, 1), // first argument is virtual time, second argument is element value
  27. next(210, 0),
  28. next(220, 1),
  29. next(230, 2),
  30. next(240, 4),
  31. completed(300) // virtual time when completed is sent
  32. ])
  33.  
  34. // `start` method will by default:
  35. // * Run the simulation and record all events
  36. // using observer referenced by `res`.
  37. // * Subscribe at virtual time 200
  38. // * Dispose subscription at virtual time 1000
  39. let res = scheduler.start { xs.map { $0 * 2 } }
  40.  
  41. let correctMessages = [
  42. next(210, 0 * 2),
  43. next(220, 1 * 2),
  44. next(230, 2 * 2),
  45. next(240, 4 * 2),
  46. completed(300)
  47. ]
  48.  
  49. let correctSubscriptions = [
  50. Subscription(200, 300)
  51. ]
  52.  
  53. XCTAssertEqual(res.events, correctMessages)
  54. XCTAssertEqual(xs.subscriptions, correctSubscriptions)
  55. }
  56. ```
  57.  
  58. ## Testing operator compositions (view models, components)
  59.  
  60. Examples of how to test operator compositions are contained inside `Rx.xcworkspace` > `RxExample-iOSTests` target.
  61.  
  62. It's easy to define `RxTests` extensions so you can write your tests in a readable way. Provided examples inside `RxExample-iOSTests` are just suggestions on how you can write those extensions, but there are a lot of possibilities on how to write those tests.
  63.  
  64. ```swift
  65. // expected events and test data
  66. let (
  67. usernameEvents,
  68. passwordEvents,
  69. repeatedPasswordEvents,
  70. loginTapEvents,
  71.  
  72. expectedValidatedUsernameEvents,
  73. expectedSignupEnabledEvents
  74. ) = (
  75. scheduler.parseEventsAndTimes("e---u1----u2-----u3-----------------", values: stringValues).first!,
  76. scheduler.parseEventsAndTimes("e----------------------p1-----------", values: stringValues).first!,
  77. scheduler.parseEventsAndTimes("e---------------------------p2---p1-", values: stringValues).first!,
  78. scheduler.parseEventsAndTimes("------------------------------------", values: events).first!,
  79.  
  80. scheduler.parseEventsAndTimes("e---v--f--v--f---v--o----------------", values: validations).first!,
  81. scheduler.parseEventsAndTimes("f--------------------------------t---", values: booleans).first!
  82. )
  83. ```
  84.  
  85. ## Integration tests
  86.  
  87. It is also possible to write integration tests by using `RxBlocking` operators.
  88.  
  89. Using `RxBlocking`'s `toBlocking()` method, you can block the current thread and wait for the sequence to complete, allowing you to synchronously access its result.
  90.  
  91. 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.
  92.  
  93. ```swift
  94. let result = try fetchResource(location)
  95. .toBlocking()
  96. .toArray()
  97.  
  98. XCTAssertEqual(result, expectedResult)
  99. ```
  100.  
  101. 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.
  102.  
  103. ```swift
  104. let result = try fetchResource(location)
  105. .toBlocking()
  106. .materialize()
  107.  
  108. // For testing the results or error in the case of terminating with error
  109. switch result {
  110. case .completed:
  111. XCTFail("Expected result to complete with error, but result was successful.")
  112. case .failed(let elements, let error):
  113. XCTAssertEqual(elements, expectedResult)
  114. XCTAssertErrorEqual(error, expectedError)
  115. }
  116.  
  117. // For testing the results in the case of termination with completion
  118. switch result {
  119. case .completed(let elements):
  120. XCTAssertEqual(elements, expectedResult)
  121. case .failed(_, let error):
  122. XCTFail("Expected result to complete without error, but received \(error).")
  123. }
  124. ```
Add Comment
Please, Sign In to add comment