我试图了解如何正确使用TestScheduler
的RxTest 4.2
为RxSwift 4.2
。
如何正确创建TestScheduler并传递它会在执行所有事件后终止调度程序的事件?
下面的代码在启动测试调度程序时进入无限循环。因为它保持同步循环,所以我没有机会从测试中停止调度程序
var testScheduler: TestScheduler = TestScheduler(initialClock: 0)
let stateObserver = testScheduler.createObserver(Bool.self)
subject.outputRelay.bind(to: stateObserver).disposed(by: bag)
let events = [next(1,true), next(10,false)]
let hotObservable = testScheduler.createHotObservable(events)
//direct events to the given relay
hotObservable.bind(to: subject.inputRelay).disposed(by: bag)
testScheduler.start() //infinite loop
//do not get called
XCTAssertEqual(stateObserver.events, [next(0, InterfaceState.started)])
testScheduler.stop()
我已经缩小了在TestScheduler
内发出的这个方法调用。订阅处理似乎有问题
func findNext() -> VirtualSchedulerItem<VirtualTime>? {
while let front = _schedulerQueue.peek() {
if front.isDisposed {
//this never gets called
_schedulerQueue.remove(front)
continue
}
return front
}
return nil
}
在这种情况下我做的是在特定时间安排一个块,我知道所有测试都应该完成并停止订阅。我认为在你的情况下应该是这样的:
//...
let hotObservable = testScheduler.createHotObservable(events)
let disposable = hotObservable.bind(to: subject.inputRelay)
testScheduler.scheduleAt(1) {
disposable.dispose()
}
testScheduler.start()
XCTAssertEqual(stateObserver.events, [next(0, InterfaceState.started)])
//...