如何使用 ocUnit 编写地理位置单元测试
我目前正在使用一个简单的对象,该对象将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 ocUnit 问题的最终解决方案是创建一个 NSDecimalNumber 并使用 STAssertTrue 与 isEqual 进行比较。希望这对其他人有帮助!
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!
我相信在比较 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.