Reachability.swift icon indicating copy to clipboard operation
Reachability.swift copied to clipboard

Behaviour on test

Open ogezue opened this issue 10 years ago • 0 comments
trafficstars

Hi I have written a Wrapper-class and a unit test and recognizes a strange behaviour under test (some kind of Volkswagening?). Under test the lib tells me, that it can reach an invalid URL. Running on simulator with an invalid URL it tells me, that the host can not be reached under Wifi but under Cellular.

Here is my wrapper with an invalid host:

import Foundation
import ReachabilitySwift

class ReachabilityHelper: NSObject {

    var isOnline = false
    private var reachability: Reachability


    override init() {

        self.reachability = try! Reachability(hostname: "anInvalidUrl")

        super.init()
        start()
    }


    private func start(){

        self.reachability.whenReachable = { reachability in

            dispatch_async(dispatch_get_main_queue()) {

                self.isOnline = true

                if reachability.isReachableViaWiFi() {
                    print("Reachable via WiFi")
                } else {
                    print("Reachable via Cellular")
                }
            }
        }
        self.reachability.whenUnreachable = { reachability in

             self.isOnline = false

            dispatch_async(dispatch_get_main_queue()) {
                print("Not reachable")
            }
        }

        do {
            try self.reachability.startNotifier()


            if(isReachable()){
                print("server is reachable")
            }
            else{
                print("server is not reachable")
            }

        } catch {
            print("Unable to start notifier")
        }
    }

    func isReachable()->Bool{

        self.isOnline =  self.reachability.isReachable()
        return self.isOnline
    }


    //MARK: - SharedInstance
    class var sharedInstance: ReachabilityHelper {
        struct Singleton {
            static let instance = ReachabilityHelper()
        }
        return Singleton.instance
    }

}

And here my test:

class ReachabilityHelperTest: XCTestCase {

    override func setUp() {
        super.setUp()
        ReachabilityHelper.sharedInstance
    }


    func testServerIsReachable() {
        let isReachable = ReachabilityHelper.sharedInstance.isReachable()

        XCTAssertTrue(isReachable)
    }
}

ogezue avatar Nov 23 '15 10:11 ogezue