This is a guide on how to write unit tests in iOS for the Place manager to ensure your logic is working.
First step is to create a wrapper around GMBLVisit object:
protocol LocationEventProtocol {
var visitID: String { get set }
}
class LocationEvent: LocatioEventProtocol {
var visitID: String = ""
}
Now we need to create a wrapper around the PlaceManager, this will emit place events from PlaceManager
protocol LocationEventManagerProtocol: PlaceManagerDelegate {
init(service: LocationEventService)
func isMonitoring() -> Bool
func startMonitoring()
func stopMonitoring()
func placeManager(_ manager: PlaceManager, didBegin visit: Visit)
}
class LocationEventManager:NSObject, LocationEventManagerProtocol {
private let service:LocationEventService
private let placeManager:PlaceManager
required init(service: LocationEventService) {
self.service = service
self.placeManager = PlaceManager()
super.init()
self.placeManager.delegate = self
}
func isMonitoring() -> Bool {
return PlaceManager.isMonitoring()
}
func startMonitoring() {
PlaceManager.startMonitoring()
}
func stopMonitoring() {
PlaceManager.stopMonitoring()
}
}
extension LocationEventManager: PlaceManagerDelegate {
func placeManager(_ manager: PlaceManager, didBegin visit: Visit) {
let locationEventVisit = LocationEventVisit()
locationEventVisit.visitID = visit.visitID
self.service.didBegin(visit: LocationEventVisit)
}
}
The next step is to create a class that would hold the logic for responding to the place events:
class LocationEventService: LocationEventServiceProtocol {
func didBegin(visit: LocationEventProtocol) {
NotificationCenter.default.post(name: .didBeginVisit, object: nil, userInfo: ["visitID": visit.visitID])
}
}
Finally here is an example of a test you could perform on the LocationEventService class:
func testExample() throws {
let testService = LocationEventService()
let handler: (Notification) -> Bool = { notification in // Mark 1
guard let visitID = notification.userInfo?["visitID"] as? String else {
XCTFail("did not emit visitID")
return false
}
XCTAssertEqual(visitID, "visitID")
return true
}
expectation(forNotification: .didBeginVisit, object: nil, handler: handler)
let locationEvent = LocationEvent()
regionVisit.visitID = "visitID"
testService.didBegin(visit: regionVisit)
waitForExpectations(timeout: 2, handler:nil)
}
Comments
0 comments
Please sign in to leave a comment.