2011-10-04 178 views
0

裏面我有以下我的表視圖控制器方法
爲什麼在運行單元測試時出現invalidArgument異常?

-(BOOL)isValidCoordinate:(CLLocationCoordinate2D)coordinate 
{ 
    //This is just to make sure exception is always thrown a cut down version of method 
    [NSException raise:@"Invalid longitude value" format:@"Longitude of %d is invalid", coordinate.longitude]; 
    return TRUE; 
} 

我正在反對一個單元測試,我也沒有給下面的異常

2011-10-04 22:58:43.380 navman2[74159:ec03] -[MapViewController isValidCoordinate:]: unrecognized selector sent to instance 0x5e20830 
2011-10-04 22:58:43.382 navman2[74159:ec03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapViewController isValidCoordinate:]: unrecognized selector sent to instance 0x5e20830' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00f555a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x010a9313 objc_exception_throw + 44 
    2 CoreFoundation      0x00f570bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00ec6966 ___forwarding___ + 966 
    4 CoreFoundation      0x00ec6522 _CF_forwarding_prep_0 + 50 
    5 navman2Tests      0x05765b41 -[navman2Tests testExceptionThrownBycoordinatesCheck] + 193 
    6 CoreFoundation      0x00ec5c7d __invoking___ + 29 
    7 CoreFoundation      0x00ec5b51 -[NSInvocation invoke] + 145 
    8 SenTestingKit      0x201043d2 -[SenTestCase invokeTest] + 69 
    9 SenTestingKit      0x20104aa7 -[SenTestCase performTest:] + 192 
    10 SenTestingKit      0x201041d3 -[SenTest run] + 88 
    11 SenTestingKit      0x20106eda -[SenTestSuite performTest:] + 115 
    12 SenTestingKit      0x201041d3 -[SenTest run] + 88 
    13 SenTestingKit      0x20106eda -[SenTestSuite performTest:] + 115 
    14 SenTestingKit      0x201041d3 -[SenTest run] + 88 
    15 SenTestingKit      0x20106eda -[SenTestSuite performTest:] + 115 
    16 SenTestingKit      0x201041d3 -[SenTest run] + 88 
    17 SenTestingKit      0x201067a4 +[SenTestProbe runTests:] + 174 
    18 Foundation       0x0092e79e __NSFireDelayedPerform + 441 
    19 CoreFoundation      0x00f368c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 
    20 CoreFoundation      0x00f37e74 __CFRunLoopDoTimer + 1220 
    21 CoreFoundation      0x00e942c9 __CFRunLoopRun + 1817 
    22 CoreFoundation      0x00e93840 CFRunLoopRunSpecific + 208 
    23 CoreFoundation      0x00e93761 CFRunLoopRunInMode + 97 
    24 GraphicsServices     0x016171c4 GSEventRunModal + 217 
    25 GraphicsServices     0x01617289 GSEventRun + 115 
    26 UIKit        0x001b5c93 UIApplicationMain + 1160 
    27 navman2        0x000028b9 main + 121 
    28 navman2        0x00002835 start + 53 
) 
terminate called throwing an exception(gdb) 

單元測試是以下幾點:

#import "navman2Tests.h" 
#import "TableViewController.h" 
#import "MapViewController.h" 


@implementation navman2Tests 

- (void)setUp 
{ 
    [super setUp]; 

    // Set-up czode here. 
} 

- (void)tearDown 
{ 
    // Tear-down code here. 

    [super tearDown]; 
} 


- (void) testExceptionThrownBycoordinatesCheck 
{ 

    TableViewController *newView2 =[[MapViewController alloc] init]; 
    CLLocationCoordinate2D coordinate; 
    double lat = 61.2180556; 
    double lng = -149.9002778; 
    coordinate.latitude = lat; 
    coordinate.longitude = lng; 
    STAssertThrows([newView2 isValidCoordinate:coordinate],@"some text description"); 
    [newView2 release]; 
} 
@end 

回答

1

錯誤在於您確實將newView2聲明爲TableViewController不是的MapViewController(其具有該方法),改變:

TableViewController *newView2 =[[MapViewController alloc] init]; 

與:

MapViewController *newView2 =[[MapViewController alloc] init]; 

反正我稱之爲可變newViewController而非newView2:P

相關問題