Search code examples
objective-cnsstringocunit

How to write a unit test for geo location with ocUnit


I'm currently working with a simple object that holds a lat and lng value as NSString. But when I attempt to assert they are equal I get a failure w/ this approach

- (void) testLocationParseJson {
  NSArray* items = //some array the represents the json returned from google ...
  LocationParseJson* sut = [[LocationParseJson alloc] init];
  Location* actualLocation = [sut parseJson:items];

  NSString* actualLatitude = actualLocation.lat;
  NSString* actualLongitude = actualLocation.lng;

  STAssertEqualObjects(expectedLocation.lat, actualLocation.lat, @"The actual location latitude was %@", actualLocation.lat);
}

here is the error shown

error: -[LocationParseJsonTest testLocationParseJson] : '41.6756668' should be equal to '41.6756668' The actual location latitude was 41.6756668

So instead I tried the AssertTrue approach with an "isEqual"

STAssertTrue([expectedLocation.lat isEqual: actualLocation.lat], @"The actual location latitude was %@", actualLocation.lat);

And I get the same error

error: -[LocationParseJsonTest testLocationParseJson] : "[expectedLocation.lat isEqual: actualLocation.lat]" should be true. The actual location latitude was 41.6756668

How should I compare these NSString values in ocUnit? here is the .h file for Location fyi

@interface Location : NSObject {
  NSString* lng;
  NSString* lat;
}

@property (nonatomic, retain) NSString* lng;
@property (nonatomic, retain) NSString* lat;

@end

Solution

  • The final solution to this ocUnit problem was to create an NSDecimalNumber and compare that using STAssertTrue with an isEqual. Hope this helps someone else!

    - (void) testLocationParseJson {
      NSString* data = @"{\"name\":\"50035\",\"Status\":{\"code\":200,\"request\":\"geocode\"},\"Placemark\":[{\"id\":\"p1\",\"address\":\"Bondurant, IA 50035, USA\",\"AddressDetails\":{\"Accuracy\":5,\"Country\":{\"AdministrativeArea\":{\"AdministrativeAreaName\":\"IA\",\"Locality\":{\"LocalityName\":\"Bondurant\",\"PostalCode\":{\"PostalCodeNumber\":\"50035\"}}},\"CountryName\":\"USA\",\"CountryNameCode\":\"US\"}},\"ExtendedData\": {\"LatLonBox\": {\"north\": 41.7947250,\"south\": 41.6631189,\"east\": -93.3662679,\"west\": -93.5322489}},\"Point\":{\"coordinates\":[-93.4805335,41.6756668,0]}}]}";
      NSArray* items = [data JSONValue];
    
      NSDecimalNumber* expectedLat = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@", @"41.6756668"]];
      NSDecimalNumber* expectedLng = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@", @"-93.4805335"]];
    
      LocationParseJson* sut = [[LocationParseJson alloc] init];
      Location* actualLocation = [sut parseJson:items];
    
      STAssertTrue([expectedLat isEqual: actualLocation.lat], @"The actual location lat was %@", actualLocation.lat);
      STAssertTrue([expectedLng isEqual: actualLocation.lng], @"The actual location lng was %@", actualLocation.lng);
    }