Swiftpack.co - Package - devxoul/RxExpect

RxExpect

Swift CocoaPods Build Status Codecov

RxExpect is a testing framework for RxSwift.

Concept

Provide inputs then test outputs. This is an example code that tests map() operator multiplying the values by 2.

func testMultiply() {
  let test = RxExpect()
  let value = PublishSubject<Int>()
  let result = value.map { $0 * 2 }

  // provide inputs
  test.input(value, [
    next(100, 1),
    next(200, 2),
    next(300, 3),
    completed(400)
  ])
  
  // test output
  test.assert(result) { events in
    XCTAssertEqual(events, [
      next(100, 2),
      next(200, 4),
      next(300, 6),
      completed(400)
    ])
  }
}

It would be easy to understand if you imagine the marble diagram.

time   --100-200-300-400 // virtual timeline
value  --1---2---3---|   // provide inputs
result --2---4---6---|   // test these values

This is more complicated example.

final class ArticleDetailViewModelTests: XCTestCase {
  func testLikeButtonSelected() {
    let test = RxExpect()
    let viewModel = ArticleDetailViewModel()
    test.retain(viewModel) // IMPORTANT: prevent from being disposed while testing

    // providing an user input: user tapped like button
    test.input(viewModel.likeButtonDidTap, [
      next(100, Void()),
    ])

    // test output: like button become selected
    test.assert(viewModel.isLikeButtonSelected) { events in
      XCTAssertEqual(events.at(100...).elements, [true])
    }
  }

  func testLikeButtonUnselected() {
    let test = RxExpect()
    let viewModel = ArticleDetailViewModel()
    test.retain(viewModel) // IMPORTANT: prevent from being disposed while testing

    // providing an user input: user tapped like button
    test.input(viewModel.likeButtonDidTap, [
      next(100, Void()),
    ])

    // test output: like button become selected
    test.assert(viewModel.isLikeButtonSelected) { events in
      XCTAssertEqual(events.at(100...).elements, [false])
    }
  }
}

Examples

APIs

Providing Inputs

  • input(observer, events)
  • input(variable, events)

Start Assertion Chaining

  • assert(source, closure)

Installation

  • For iOS 8+ projects with CocoaPods:

    pod 'RxExpect'
    
  • For iOS 8+ projects with Carthage:

    github "devxoul/RxExpect"
    

Development

$ swift package generate-xcodeproj
$ open RxExpect.xcodeproj

License

RxExpect is under MIT license. See the LICENSE file for more info.

Github

link
Stars: 86

Dependencies

Used By

Total: 0

Releases

- 2019-06-14 07:29:32

  • Add Swift 5 support 🚀 (#15)

- 2018-09-27 08:32:34

  • Add Swift 4.2 support 🎉 (#10, @murselturk)

- 2017-12-21 19:06:25

  • Add an available to dispose test manually (fc804bf7e0681ee35baf7d03d959d02c10d61245)
  • Wait for async tests (2bfe80e90efbfb22038880b5e984211da2a493db)

- 2017-10-18 16:05:17

  • Rewrite everything.
  • Add Swift 4 and RxSwift 4 support.

- 2017-05-31 08:46:25

  • Add support to retain objects to prevent from being released (18a2c5cb973b04bd04a7f0aca45fc247ef689e91)

- 2017-05-30 17:25:28

  • Use official RxSwift release

- 2017-05-20 18:45:56

  • Add support asserting count (#4)
  • Add support for multiple assertions in a single expectation (#5)

- 2017-05-07 15:04:13

  • Add temporary support for Swift Package Manager (61475bdfb70775b6e07463026b178c9faaec874d)

- 2017-04-30 19:00:34

  • Make scheduler open (19cf626dc69e5e227bf94f11b515dd116a577ebc)

- 2017-04-27 08:31:06

  • Fix not() behavior (d5d9548d330cc03b5fcc4a98834ec37e4fe41033)

- 2017-04-26 19:04:59

  • Add temporary support for Swift Package Manager (26984cade85cb84f4062d90837200eb4fe845e42)

- 2017-04-23 20:45:46

  • Drop tvOS support

- 2017-03-21 14:46:32

  • Make disposeBag open
  • Use test method name as default behavior message

- 2017-03-21 14:46:15

  • Add not() to reverse result
  • Add contains() assertion

- 2017-01-19 16:54:45

  • Add support testing equality of Sequence (#3, @DriesSchulten)

- 2017-01-15 20:20:06

  • Redesign the assertion interface

- 2016-10-31 14:19:53

  • Supports Swift 3 :tada:

- 2016-10-31 14:10:01

  • Use XCTestCase instead of RxTestCase

- 2016-10-31 14:09:51

Hello, world!

- 2016-10-31 14:09:01

  • Fix assertion

Test Release - 2016-08-09 08:09:59