1

我最近从 iOS 5 升级到 iOS 6,发现这个单元测试失败了

- (void)testCalculatesDistanceBetweenTwoPoints
{
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualObjects(distance, expected, @"");
}

失败的断言

'3822.23073702318' 应该等于 '3822.23073702318'

当我打印每个数字的原始值时,我得到的值看起来相同

2012-10-23 20:01:42.970 HelloWorld[1573:c07] 1 3822.23073702318
2012-10-23 20:01:42.970 HelloWorld[1573:c07] 2 3822.23073702318

当我打印每个数字的类型时,我得到的结果看起来是一样的(我做错了吗?)

2012-10-23 20:06:37.309 HelloWorld[1611:c07] 1 __NSCFNumber 
2012-10-23 20:06:37.309 HelloWorld[1611:c07] 2 __NSCFNumber

如果有帮助,这是完整的实现

- (NSNumber *)kilometresBetweenPlace1:(CLLocationCoordinate2D)place1 andPlace2:(CLLocationCoordinate2D)place2
{
    MKMapPoint start = MKMapPointForCoordinate(place1);
    MKMapPoint finish = MKMapPointForCoordinate(place2);

    double distance = MKMetersBetweenMapPoints(start, finish) / 1600;

    return [NSNumber numberWithDouble:distance];
}
4

1 回答 1

0

对于任何可能关注此讨论的人-这就是我最终得到的

- (void)testCalculatesDistanceBetweenTwoPoints
{
    double accuracy = 0.00000000001;
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualsWithAccuracy([distance doubleValue], [expected doubleValue], accuracy, @"");
}
于 2012-10-24T02:10:48.560 回答