为什么我在运行单元测试时会收到invalidArgument异常?

时间:2011-10-04 12:09:01

标签: objective-c unit-testing xcode4

在我的表视图控制器中,我有以下方法

-(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 个答案:

答案 0 :(得分:1)

错误是您确实将newView2声明为 TableViewController 而不是 MapViewController (具有该方法),更改:

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

使用:

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

无论如何我会调用变量 newViewController 而不是 newView2 :P