Skip to content

Commit b072403

Browse files
author
Jiang Xiaoming
committed
添加单体测试
1 parent a3c53ab commit b072403

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

RxTestDemo/RxTestDemoTests/RxTestDemoTests.swift

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,98 @@
77
//
88

99
import XCTest
10+
import RxTest
11+
import RxSwift
12+
1013
@testable import RxTestDemo
1114

1215
class RxTestDemoTests: XCTestCase {
13-
16+
let viewModel = ViewModel()
17+
var disposeBag = DisposeBag()
1418
override func setUp() {
15-
// Put setup code here. This method is called before the invocation of each test method in the class.
1619
}
1720

1821
override func tearDown() {
1922
// Put teardown code here. This method is called after the invocation of each test method in the class.
2023
}
24+
25+
func testAdd() {
26+
let scheduler = TestScheduler(initialClock: 0)
2127

22-
func testExample() {
23-
// This is an example of a functional test case.
24-
// Use XCTAssert and related functions to verify your tests produce the correct results.
25-
}
28+
let textColorObserver = scheduler.createObserver(UIColor.self)
29+
self.viewModel.rx_numberLabelTextColor
30+
.subscribe(textColorObserver)
31+
.disposed(by: disposeBag)
32+
33+
let textObserver = scheduler.createObserver(String.self)
34+
self.viewModel.rx_numberLabelText
35+
.subscribe(textObserver)
36+
.disposed(by: disposeBag)
37+
38+
let inputNumber = scheduler.createHotObservable([Recorded.next(100, (1)),
39+
Recorded.next(200, (1)),
40+
Recorded.next(300, (1)),
41+
Recorded.next(400, (1)),
42+
Recorded.next(500, (1)),
43+
Recorded.next(600, (1)),
44+
Recorded.next(700, (1)),
45+
Recorded.next(800, (1)),
46+
Recorded.next(900, (1)),
47+
Recorded.next(1000, (1)),
48+
Recorded.next(1100, (1)),
49+
Recorded.next(1200, (-1)),
50+
Recorded.next(1300, (-1)),
51+
])
2652

27-
func testPerformanceExample() {
28-
// This is an example of a performance test case.
29-
self.measure {
30-
// Put the code you want to measure the time of here.
31-
}
32-
}
53+
inputNumber.subscribe(onNext: { [weak self](n) in
54+
if(n == 1){
55+
self?.viewModel.addButtonAction()
56+
}
57+
else{
58+
self?.viewModel.subtractButtonAction()
59+
}
60+
}).disposed(by: disposeBag)
61+
62+
scheduler.start()
63+
64+
let expectedtextEvents = [
65+
Recorded.next(0, ("当前数字: 0")),
66+
Recorded.next(100, ("当前数字: 1")),
67+
Recorded.next(200, ("当前数字: 2")),
68+
Recorded.next(300, ("当前数字: 3")),
69+
Recorded.next(400, ("当前数字: 4")),
70+
Recorded.next(500, ("当前数字: 5")),
71+
Recorded.next(600, ("当前数字: 6")),
72+
Recorded.next(700, ("当前数字: 7")),
73+
Recorded.next(800, ("当前数字: 8")),
74+
Recorded.next(900, ("当前数字: 9")),
75+
Recorded.next(1000, ("当前数字: 10")),
76+
Recorded.next(1100, ("当前数字: 11")),
3377

78+
Recorded.next(1200, ("当前数字: 10")),
79+
Recorded.next(1300, ("当前数字: 9"))]
80+
81+
XCTAssertEqual(textObserver.events, expectedtextEvents)
82+
83+
let expectedtextColorEvents = [
84+
Recorded.next(0, (UIColor.black)),
85+
Recorded.next(100, (UIColor.black)),
86+
Recorded.next(200, (UIColor.black)),
87+
Recorded.next(300, (UIColor.black)),
88+
Recorded.next(400, (UIColor.black)),
89+
Recorded.next(500, (UIColor.black)),
90+
Recorded.next(600,(UIColor.black)),
91+
Recorded.next(700, (UIColor.black)),
92+
Recorded.next(800, (UIColor.black)),
93+
Recorded.next(900, (UIColor.black)),
94+
Recorded.next(1000, (UIColor.black)),
95+
Recorded.next(1100, (UIColor.red)),
96+
97+
Recorded.next(1200, (UIColor.black)),
98+
Recorded.next(1300, (UIColor.black)),
99+
100+
]
101+
XCTAssertEqual(textColorObserver.events, expectedtextColorEvents)
102+
103+
}
34104
}

0 commit comments

Comments
 (0)