2

我试图了解如何正确使用TestSchedulerfrom RxTest 4.2for 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
    }
4

1 回答 1

1

在这种情况下,我所做的是在我知道所有测试都应该完成并停止订阅的特定时间安排一个块。我认为在你的情况下应该是这样的:

//...
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)])
//...
于 2018-09-03T09:40:31.497 回答