qqq
qqq

Reputation: 1410

Creating CLPlacemark with custom values for testing

I have an application that moves CLPlacemark objects around and uses them, and I would like to unit-test several components that interact with them. To do this, I would like to be able to stub out calls to real reverse-geolocation from MapKit with methods that produce mock CLPlacemarks with known values.

CLPlacemark only has one initializer (copy initializer). But in the documentation, it says:

Placemark objects are typically generated by a CLGeocoder object, although you can also create them explicitly yourself.

However, most of the members are read-only, so I'm not sure how to create one with custom values. Is it possible to set internal properties in this way in Swift? If not, any ideas as to what they mean in the above citation?

Upvotes: 6

Views: 1160

Answers (2)

Thibaud David
Thibaud David

Reputation: 504

Some archeology here, but here is a working answer, as I just had this need

    public static func mock(coordinate: CLLocationCoordinate2D, name: String) -> CLPlacemark {
        let mkPlacemark = MKPlacemark(
            coordinate: coordinate,
            addressDictionary: ["name": name]
        )
        return CLPlacemark(placemark: mkPlacemark)
    }

Usage:

let placemark = CLPlacemark.mock(
    coordinate: CLLocation(latitude: 0, longitude: 0), name: "mock-name"
)

Upvotes: 1

rounak
rounak

Reputation: 9387

I would use OCMock (http://ocmock.org) to stub out calls to create stub CLPlacemark objects, and stub out their getter methods with your own values.

id userDefaultsMock = OCMClassMock([CLPlacemark class]);

// set it up to return a specific value when stringForKey: is called
OCMStub([userDefaultsMock property]).andReturn(customValue);

Upvotes: 0

Related Questions