iosswiftcore-locationclplacemark

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?


Solution

  • 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);