So ive been using Rxswift for a while and its been working well. Ive managed to get all my code under test but Im struggling to figure out how to test searching with searchbar.rx.bindTo .
There are many tutorials of how to use RxSwift for searching and returning results on a tableview but in none of those tutorials do they show you how to unit test it. https://www.thedroidsonroids.com/blog/ios/rxswift-by-examples-1-the-basics/
The above linked shows what im trying to achieve with the searchbar and populating the TableView.
Ive tried testing it with RxBlocking but my tests all seem to hang. systemUnderTest is the viewModel results is the Observable<[T]> that comes back from the service.
let results = systemUnderTest.results.toBlocking()
let noneObservableList = try! results.single()
//Then
XCTAssert(noneObservableList?.count == expectedCount)
It hangs on the try! results.single() and never hits the assert. Anyone know how to test this.
Thanks in advance.
This is systemUnderTest:
public class SearchViewModel: SearchViewModelContract {
public var query: Variable<String?> = Variable(String.EmptyString())
public var results: Observable<[ThirdPartySite]>
let minimumCharacterCount = 4
let dueTime = 0.3
let disposeBag = DisposeBag()
public init() {
results = Observable.just([Object]())
results = query.asObservable().throttle(dueTime, scheduler: MainScheduler.instance).flatMapLatest{
queryString -> Observable<Object> in
if let queryString = queryString {
if queryString.characters.count >= self.minimumCharacterCount {
return self.Something(siteName: queryString)
}
return Observable.just(Object(in: Object()))
}
return Observable.just(Object(in: Object()))
}.map { results in
return results.items
}.catchErrorJustReturn([Object]()).shareReplay(1)
}
}