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

发布于 2024-12-07 17:03:00 字数 3951 浏览 0 评论 0原文

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

-(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

Inside my Table view controller I have following method

-(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;
}

I am running a unit test against it and I it failing giving following exception

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) 

Unit test is following:

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-14 17:03:00

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

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

with:

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

无论如何我都会调用变量 newViewController< /strong> 而不是 newView2 :P

The error is that you did declare newView2 as a TableViewController not a MapViewController (which has that method), change:

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

with:

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

anyway I would call the variable newViewController rather than newView2 :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文