如何使用 ocUnit 编写地理位置单元测试

发布于 2024-10-15 07:09:54 字数 1272 浏览 10 评论 0原文

我目前正在使用一个简单的对象,该对象将 lat 和 lng 值保存为 NSString。但是当我尝试断言它们相等时,我遇到了失败,这种方法

- (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);
}

显示错误

: -[LocationParseJsonTest testLocationParseJson] :'41.6756668'应该等于'41.6756668'实际位置纬度是41.6756668

所以我尝试了使用“isEqual”的 AssertTrue 方法

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

我得到了相同的错误

错误: -[LocationParseJsonTest testLocationParseJson] :“[expectedLocation.lat isEqual:actualLocation.lat]”应该是 true。实际位置纬度是 41.6756668

我应该如何比较 ocUnit 中的这些 NSString 值?这是位置的 .h 文件仅供参考

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

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

@end

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墟烟 2024-10-22 07:09:54

此 ocUnit 问题的最终解决方案是创建一个 NSDecimalNumber 并使用 STAssertTrue 与 isEqual 进行比较。希望这对其他人有帮助!

- (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);
}

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);
}
哑剧 2024-10-22 07:09:54

我相信在比较 NSString 时,您应该使用 isEqualToString 方法来比较它们。

尝试:
STAssertTrue([expectedLocation.lat isEqualToString:actualLocation.lat], @"实际位置纬度为 %@",actualLocation.lat);

编辑:这个 StackOverflow 问题 对于为什么你的方法不起作用有一些可能的解释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文