I'm attempting to write a unit test for Driver
from RxCocoa library. Here's my simplified implementation code:
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)
}
}
}
And here's the Quick/Nimble unit test I'm attempting to pass:
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)
Above expect
says that capturedLoginResult
is still .failed
. It appears as though element from return latestCredentials.flatMapLatest { (username, password) in .just(.success) }
is not getting received by the .drive(onNext: )
in the test.
If the implementation of login
is just:
func login() -> Driver<LoginResult> {
return .just(.success)
}
The test passes.
Any thoughts on what's happening here? Thanks!