mercredi 22 juin 2016

Write Unit Tests for RxSwift

I got this class which I'd like to write tests for:

import CoreLocation
import RxCocoa
import RxSwift

struct LocationManager {

    private (set) var authorized: Driver<Bool>
    private let coreLocationManager = CLLocationManager()

    init() {
        coreLocationManager.distanceFilter = kCLDistanceFilterNone
        coreLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation

        authorized = Observable.deferred { [weak coreLocationManager] in
            let status = CLLocationManager.authorizationStatus()
            guard let coreLocManager = coreLocationManager else {
                return Observable.just(status)
            }
            return coreLocManager
                .rx_didChangeAuthorizationStatus
                .startWith(status)
            }
            .asDriver(onErrorJustReturn: CLAuthorizationStatus.NotDetermined)
            .map {
                switch $0 {
                case .AuthorizedWhenInUse:
                    return true
                default:
                    return false
                }
        }

        coreLocationManager.requestWhenInUseAuthorization()
    }
}

Basically I want to test whether the authorized Driver has the correct value based on possible CLAuthorizationStatuses. I need a hint in the right direction since I am not familiar with unit testing with RxSwift. I guess my best option is to create a mock of CLLocationManager which returns some CLAuthorizationStatus when authorizationStatus() is called and afterwards I would check the value of the authorized Driver right ?

Any explanation on how to test this LocationManager class is appreciated.

Aucun commentaire:

Enregistrer un commentaire