swiftunit-testingclbeacon

Mock CLBeacon Object in Swift


I have been searching online for a way to mock a CLBeacon object in order to test one of my classes. All I can find is related to COMockito which is only compatible with Objective-c.

All I need, is to be able to create a mock CLBeacon and set its major minor and UUID values. The problem is that these parameters are read only.

Any idea on what I could do to have a mock CLBeacon with parameters that I set?


Solution

  • I'm not sure why I always forget about NSCoding, but you can doctor up CLBeacon objects using KVO syntax. I finally got unit testing working with CLBeacon like this:

        let mockUUID = NSUUID(UUIDString: uuidString)
        let testBeacon = CLBeacon()
        testBeacon.setValue(testMajor, forKey: "major")
        testBeacon.setValue(testMinor, forKey: "minor")
        testBeacon.setValue(mockUUID, forKey: "proximityUUID")
        testBeacon.setValue(CLProximity.Far.rawValue, forKey: "proximity")