2

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
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
  • 1
    Are you positive that the two objects are actually instances of NSString? Perhaps you should expand your failure message to include [expectedLocation.lat class] & [actualLocation.lat class]. – retainCount Feb 03 '11 at 01:17
  • @retainCount you are the winner sir, turns out the actual is NSDecimalNumber and the expected is NSCFString ... so how do I cast it to be string instead? thank you sir! – Toran Billups Feb 03 '11 at 01:35
  • 1
    You don’t cast it to be a string. You compare it appropriately for decimal numbers (e.g. with a tolerance). – Chris Hanson Feb 03 '11 at 23:45

2 Answers2

2

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);
}
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
1

I believe when comparing NSStrings you should be using the isEqualToString method to compare them.

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

EDIT: The first answer in this StackOverflow question has some possible explanations as to why your way didn't work.

Community
  • 1
  • 1
arrtchiu
  • 1,076
  • 1
  • 10
  • 23
  • Thanks for the link but still no luck w/ the isEqualToString approach – Toran Billups Feb 03 '11 at 01:13
  • Is it possible that some other invisible characters are making it into one of your strings, causing it to fail comparison? Maybe put both into `NSData` objects and print their description via `NSLog` to compare the "hex dump" of each? – arrtchiu Feb 03 '11 at 01:20