我正在尝试为Driver
RxCocoa 库编写一个单元测试。这是我的简化实现代码:
struct LoginViewModel {
var username: Driver<String?>!
var password: Driver<String?>!
var loginTaps: Driver<Void>!
func login() -> Driver<LoginResult> {
let credentials = Driver.combineLatest(username, password) { ($0, $1) }
let latestCredentials = loginTaps.withLatestFrom(credentials)
return latestCredentials.flatMapLatest { (username, password) in
.just(.success)
}
}
}
这是我试图通过的 Quick/Nimble 单元测试:
let disposeBag = DisposeBag()
var capturedLoginResult = LoginResult.failed
loginViewModel.username = Driver.just("some username")
loginViewModel.password = Driver.just("some password")
loginViewModel.loginTaps = Driver.just()
loginViewModel.login().drive(onNext: { loginResult in
capturedLoginResult = loginResult
}).addDisposableTo(disposeBag)
expect(capturedLoginResult == .success)
上面expect
说capturedLoginResult
的还是.failed
。似乎元素 from在测试return latestCredentials.flatMapLatest { (username, password) in .just(.success) }
中没有被接收.drive(onNext: )
。
如果执行login
只是:
func login() -> Driver<LoginResult> {
return .just(.success)
}
测试通过。
对这里发生的事情有任何想法吗?谢谢!