2012-10-24 75 views
1

我最近從iOS 5的升級到iOS 6,發現這個單元測試失敗如何比較NSNumber與十進制值?

​​

失敗斷言

'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

檢查非整數的相等性總是風險,由於舍入誤差。你應該檢查一下,該值是否在一定範圍內。 – vikingosegundo

+0

我確實看到一個ocunit方法「STAssertEqualsWithAccuracy」 - 有人用這個成功了嗎? –

+0

@CodaFi爲什麼浮動值而不是雙倍?這不會削減一噸的準確性嗎? –

回答

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, @""); 
}