Latest 1.0.0
Homepage https://github.com/devxoul/RxExpect
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, requires ARC
Dependencies RxSwift, RxCocoa, RxTest
Frameworks XCTest
Authors

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.values, [
      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.

Latest podspec

{
    "name": "RxExpect",
    "version": "1.0.0",
    "summary": "The RxSwift testing framework",
    "homepage": "https://github.com/devxoul/RxExpect",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Suyeol Jeon": "[email protected]"
    },
    "source": {
        "git": "https://github.com/devxoul/RxExpect.git",
        "tag": "1.0.0"
    },
    "source_files": "Sources/**/*.swift",
    "requires_arc": true,
    "frameworks": "XCTest",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "dependencies": {
        "RxSwift": [
            ">= 4.0.0"
        ],
        "RxCocoa": [
            ">= 4.0.0"
        ],
        "RxTest": [
            ">= 4.0.0"
        ]
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This