RxExpect
RxExpect 是一个用于 RxSwift 的测试框架。
概念
提供输入然后测试输出。以下是一个测试 map()
操作符将值乘以 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)
])
}
}
如果你想象一下大理石图,那么这将更容易理解。
time --100-200-300-400 // virtual timeline
value --1---2---3---| // provide inputs
result --2---4---6---| // test these values
这是一个更复杂的例子。
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])
}
}
}
示例
APIs
提供输入
input(observer, events)
输入(变量,事件)
开始断言链
断言(来源,闭包)
安装
开发
$ swift package generate-xcodeproj
$ open RxExpect.xcodeproj
许可
RxExpect 采用 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。