gpt4 book ai didi

ios - 如何使用 RxTest 测试 combineLatest observable?

转载 作者:可可西里 更新时间:2023-11-01 06:13:21 25 4
gpt4 key购买 nike

所以我有这个 viewModel,它有一个验证可观察值,它只是将 5 个其他信号组合成一个 bool 值。

import RxSwift

class SchedulingFormViewModel: BaseViewModel {

let places = Variable<[String]>([])

var formIsValid: Observable<Bool>!

override init() {
super.init()
places.value = ["LUGAR 1", "LUGAR 2", "LUGAR 3"]

formIsValid = Observable.combineLatest(UserSession.currenctScheduling.dateSignal.asObservable(),
UserSession.currenctScheduling.carSignal.asObservable(),
UserSession.currenctScheduling.locationSignal.asObservable(),
UserSession.currenctScheduling.servicesSignal.asObservable())
{ (date: Date?, car: Car?, location: String?, services: [Service]?) in
print("DATE: \(date), CAR: \(car), LOCATION: \(location), SERVICES:\(services)")
guard let servicesArray = services else { return false }
let valid = date != nil && car != nil && location != nil && servicesArray.count > 0
return valid
}
}
}

然后我的测试类中有一个方法应该只测试信号是否发生变化。我尝试了多种方法,但最终都没有真正获得真正的值(value)。可观察对象仅在测试运行后发出真实信号。

我的测试类是这样设置的

import XCTest

import RxSwift
import RxTest

@testable import Automobi

class SchedulingUnitTests: XCTestCase {

var viewModel: SchedulingFormViewModel!
var disposeBag: DisposeBag!
var scheduler: TestScheduler!

override func setUp() {
super.setUp()
viewModel = SchedulingFormViewModel()
UserSession.clearScheduling()
disposeBag = DisposeBag()
scheduler = TestScheduler(initialClock: 0)
}
}

我试过 TestScheduler

func testFormValidation() {
UserSession.currenctScheduling.dateSignal.value = Date()
UserSession.currenctScheduling.carSignal.value = Car()
UserSession.currenctScheduling.servicesSignal.value = [Service(), Service()]

let location = scheduler.createHotObservable([
next(100, "TEST")
])

location.subscribe(onNext: { (text) in
print("LOCATION: \(text)")
UserSession.currenctScheduling.locationSignal.value = text
}).addDisposableTo(disposeBag)


let results = scheduler.createObserver(Bool.self)

scheduler.scheduleAt(0) {
self.viewModel.formIsValid.subscribe(results).addDisposableTo(self.disposeBag)
}

let expected = [
next(100, true)
]

scheduler.start()

XCTAssertEqual(results.events, expected)
}

还有

func testFormValidation() {
UserSession.currenctScheduling.dateSignal.value = Date()
UserSession.currenctScheduling.carSignal.value = Car()
UserSession.currenctScheduling.servicesSignal.value = [Service(), Service()]

let location = scheduler.createHotObservable([
next(100, "TEST"),
next(150, "TEST 2"),
next(200, "TEST 3")
])

location.subscribe(onNext: { (text) in
print("LOCATION: \(text)")
UserSession.currenctScheduling.locationSignal.value = text
}).addDisposableTo(disposeBag)


var results = [Bool]()
viewModel.formIsValid.subscribe(onNext: { (value) in
results.append(value)
}).addDisposableTo(disposeBag)

let expected = [true, true, true]

scheduler.start()

XCTAssertEqual(results, expected)
}

我尝试过将我的 formIsValid 绑定(bind)到一个变量并在最后验证它的值。

func testFormValidation() {
UserSession.currenctScheduling.dateSignal.value = Date()
UserSession.currenctScheduling.carSignal.value = Car()
UserSession.currenctScheduling.locationSignal.value = "TESTE"
UserSession.currenctScheduling.servicesSignal.value = [Service(), Service()]

sleep(5)

XCTAssertTrue(viewModel.formIsValid.value)
}

但是我从来没有得到预期的结果。在所有测试都失败并且代码返回执行后,我确实得到了一个真实信号。此外,在运行应用程序时,代码会按预期执行,我只需要在测试中捕获它即可。有什么想法吗?!

最佳答案

第一次尝试时,您可能会在时钟 0 上获得一个值,因此您的预期值应该是:

let expected = [
next(0, false)
next(100, true)
]

关于ios - 如何使用 RxTest 测试 combineLatest observable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com