我如何断言使用最新文本输入的可观察对象的输出
How can I assert the output of an observable that uses latest from text inputs
我的视图模型上有一个 属性:
let isValid: Driver<Bool>
let credentials: Driver<(String, String)>
......
credentials = .combineLatest(bindings.username, bindings.password, resultSelector: { (username, password) -> (String, String) in (username, password) })
isValid = credentials.map { username, password in username.count > 0 && password.count > 7 }
我想断言,当设置有效输入时 isValid
上设置了正确的状态。
我的测试在下方通过,但是感觉这不是测试此场景的正确方法。
理想情况下,我希望以 ""
作为我的字符串开始,然后传入值,就好像它们已被键入一样,这样我就可以断言默认状态已设置,然后更改。
我也找到了这些行:
.do(onNext: { state in
if state {
exp.fulfill()
}
})
有点"hacky".
func test_is_valid_state_changes_when_inputs_correct_length() {
let username: Driver<String> = .of("some_user_name")
let password: Driver<String> = .of("some_user_password")
let bindings = LoginViewModel.Bindings(username: username, password: password, loginTap: .empty(), doneTap: .empty())
let sut = LoginViewModel(dependency: "", bindings: bindings)
let scheduler = TestScheduler(initialClock: 0)
let observer = scheduler.createObserver(Bool.self)
let exp = expectation(description: "isValid Event")
sut.isValid
.asObservable()
.do(onNext: { state in
if state {
exp.fulfill()
}
})
.subscribe(observer)
.disposed(by: disposeBag)
scheduler.start()
waitForExpectations(timeout: 0.5) { error in
XCTAssertNil(error)
XCTAssertEqual(observer.events.count, 1)
XCTAssertTrue(observer.events[0].value.element!) // swiftlint:disable:this force_unwrapping
}
}
您需要使用 TestObservables 来进行您想要的测试,并且您不需要期望对象,因为此测试将在没有任何线程问题的情况下完成。
func test_is_valid_state_changes_when_inputs_correct_length() {
let scheduler = TestScheduler(initialClock: 0)
let username = scheduler.createHotObservable([.next(0, ""), .next(10, "h")])
let password = scheduler.createHotObservable([.next(0, ""), .next(30, "p"), .next(40, "passwor"), .next(50, "password")])
let bindings = LoginViewModel.Bindings(
username: username.asDriver(onErrorRecover: { _ in XCTFail(); return .empty() }),
password: password.asDriver(onErrorRecover: { _ in XCTFail(); return .empty() }),
loginTap: .empty(),
doneTap: .empty()
)
let disposeBag = DisposeBag()
let sut = LoginViewModel(dependency: "", bindings: bindings)
let observer = scheduler.createObserver(Bool.self)
sut.isValid
.drive(observer)
.disposed(by: disposeBag)
scheduler.start()
XCTAssertEqual(observer.events, [
.next(0, false),
.next(10, false),
.next(30, false),
.next(40, false),
.next(50, true)
])
}
我的视图模型上有一个 属性:
let isValid: Driver<Bool>
let credentials: Driver<(String, String)>
......
credentials = .combineLatest(bindings.username, bindings.password, resultSelector: { (username, password) -> (String, String) in (username, password) })
isValid = credentials.map { username, password in username.count > 0 && password.count > 7 }
我想断言,当设置有效输入时 isValid
上设置了正确的状态。
我的测试在下方通过,但是感觉这不是测试此场景的正确方法。
理想情况下,我希望以 ""
作为我的字符串开始,然后传入值,就好像它们已被键入一样,这样我就可以断言默认状态已设置,然后更改。
我也找到了这些行:
.do(onNext: { state in
if state {
exp.fulfill()
}
})
有点"hacky".
func test_is_valid_state_changes_when_inputs_correct_length() {
let username: Driver<String> = .of("some_user_name")
let password: Driver<String> = .of("some_user_password")
let bindings = LoginViewModel.Bindings(username: username, password: password, loginTap: .empty(), doneTap: .empty())
let sut = LoginViewModel(dependency: "", bindings: bindings)
let scheduler = TestScheduler(initialClock: 0)
let observer = scheduler.createObserver(Bool.self)
let exp = expectation(description: "isValid Event")
sut.isValid
.asObservable()
.do(onNext: { state in
if state {
exp.fulfill()
}
})
.subscribe(observer)
.disposed(by: disposeBag)
scheduler.start()
waitForExpectations(timeout: 0.5) { error in
XCTAssertNil(error)
XCTAssertEqual(observer.events.count, 1)
XCTAssertTrue(observer.events[0].value.element!) // swiftlint:disable:this force_unwrapping
}
}
您需要使用 TestObservables 来进行您想要的测试,并且您不需要期望对象,因为此测试将在没有任何线程问题的情况下完成。
func test_is_valid_state_changes_when_inputs_correct_length() {
let scheduler = TestScheduler(initialClock: 0)
let username = scheduler.createHotObservable([.next(0, ""), .next(10, "h")])
let password = scheduler.createHotObservable([.next(0, ""), .next(30, "p"), .next(40, "passwor"), .next(50, "password")])
let bindings = LoginViewModel.Bindings(
username: username.asDriver(onErrorRecover: { _ in XCTFail(); return .empty() }),
password: password.asDriver(onErrorRecover: { _ in XCTFail(); return .empty() }),
loginTap: .empty(),
doneTap: .empty()
)
let disposeBag = DisposeBag()
let sut = LoginViewModel(dependency: "", bindings: bindings)
let observer = scheduler.createObserver(Bool.self)
sut.isValid
.drive(observer)
.disposed(by: disposeBag)
scheduler.start()
XCTAssertEqual(observer.events, [
.next(0, false),
.next(10, false),
.next(30, false),
.next(40, false),
.next(50, true)
])
}