OCMock:为什么在尝试调用UIWebView模拟时出现无法识别的选择器异常? [英] OCMock: Why do I get an unrecognized selector exception when attempting to call a UIWebView mock?

查看:45
本文介绍了OCMock:为什么在尝试调用UIWebView模拟时出现无法识别的选择器异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这都是由我的其他链接标志设置中的错字引起的.请参阅我在下面的答案以获取更多信息.

This was all caused by a typo in my Other Link Flags setting. See my answer below for more information.

我正在尝试模拟UIWebView,以便可以验证在测试iOS视图控制器期间是否调用了其中的方法.我正在使用从SVN修订版70(此问题发布时的最新版本)构建的OCMock静态库,以及Google工具箱(Mac版)的Google工具箱(GTM),从SVN修订版410.当视图控制器尝试调用期望的方法时,出现以下错误.

I'm attempting to mock a UIWebView so that I can verify that methods on it are called during a test of an iOS view controller. I'm using an OCMock static library built from SVN revision 70 (the most recent as of the time of this question), and Google Toolbox for Mac's (GTM) unit testing framework, revision 410 from SVN. I'm getting the following error when the view controller attempts to call the expected method.

Test Case '-[FirstLookViewControllerTests testViewDidLoad]' started.
2010-11-11 07:32:02.272 Unit Test[38367:903] -[NSInvocation getArgumentAtIndexAsObject:]: unrecognized selector sent to instance 0x6869ea0
2010-11-11 07:32:02.277 Unit Test[38367:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSInvocation getArgumentAtIndexAsObject:]: unrecognized selector sent to instance 0x6869ea0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x010cebe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x012235c2 objc_exception_throw + 47
    2   CoreFoundation                      0x010d06fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01040366 ___forwarding___ + 966
    4   CoreFoundation                      0x0103ff22 _CF_forwarding_prep_0 + 50
    5   Unit Test                           0x0000b29f -[OCMockRecorder matchesInvocation:] + 216
    6   Unit Test                           0x0000c1c1 -[OCMockObject handleInvocation:] + 111
    7   Unit Test                           0x0000c12a -[OCMockObject forwardInvocation:] + 43
    8   CoreFoundation                      0x01040404 ___forwarding___ + 1124
    9   CoreFoundation                      0x0103ff22 _CF_forwarding_prep_0 + 50
    10  Unit Test                           0x0000272a -[MyViewController viewDidLoad] + 100
    11  Unit Test                           0x0000926c -[MyViewControllerTests testViewDidLoad] + 243
    12  Unit Test                           0x0000537f -[SenTestCase invokeTest] + 163
    13  Unit Test                           0x000058a4 -[GTMTestCase invokeTest] + 146
    14  Unit Test                           0x0000501c -[SenTestCase performTest] + 37
    15  Unit Test                           0x000040c9 -[GTMIPhoneUnitTestDelegate runTests] + 1413
    16  Unit Test                           0x00003a87 -[GTMIPhoneUnitTestDelegate applicationDidFinishLaunching:] + 197
    17  UIKit                               0x00309253 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252
    18  UIKit                               0x0030b55e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    19  UIKit                               0x0030aef0 -[UIApplication _run] + 452
    20  UIKit                               0x0031742e UIApplicationMain + 1160
    21  Unit Test                           0x0000468c main + 104
    22  Unit Test                           0x000026bd start + 53
    23  ???                                 0x00000002 0x0 + 2
)
terminate called after throwing an instance of 'NSException'
/Users/gjritter/src/google-toolbox-for-mac-read-only/UnitTesting/RunIPhoneUnitTest.sh: line 151: 38367 Abort trap              "$TARGET_BUILD_DIR/$EXECUTABLE_PATH" -RegisterForSystemEvents

我的测试代码是:

- (void)testViewDidLoad {
    MyViewController *viewController = [[MyViewController alloc] init];

    id mockWebView = [OCMockObject mockForClass:[UIWebView class]];
    [[mockWebView expect] setDelegate:viewController];

    viewController.webView = mockWebView;

    [viewController viewDidLoad];
    [mockWebView verify];
    [mockWebView release];
}

我的视图控制器代码是:

My view controller code is:

- (void)viewDidLoad {
    [super viewDidLoad];
    webView.delegate = self;
}

我确实发现,如果我改用以下方法,则测试可以成功运行:

I did find that the test would run successfully if I instead used:

- (void)testViewDidLoad {
    MyViewController *viewController = [[MyViewController alloc] init];

    id mockWebView = [OCMockObject partialMockForObject:[[UIWebView alloc] init]];
    //[[mockWebView expect] setDelegate:viewController];

    viewController.webView = mockWebView;

    [viewController viewDidLoad];
    [mockWebView verify];
    [mockWebView release];
}

但是,一旦我添加了被注释掉的期望,使用部分模拟时就会返回错误.

However, as soon as I added the expectation that is commented out, the error returned when using the partial mock.

我有其他测试已在同一项目中成功使用模拟.

I have other tests that are successfully using mocks in the same project.

有什么想法吗?OCMock是否支持模拟UIKit对象?

Any ideas? Is mocking of UIKit objects supported by OCMock?

编辑:基于以下答案的建议,我尝试了以下测试,但出现了相同的错误:

Based on advice in the answer below, I tried the following test, but I'm getting the same error:

- (void)testViewDidLoadLoadsWebView {
    MyViewController *viewController = [[MyViewController alloc] init];
    UIWebView *webView = [[UIWebView alloc] init];

    // This test fails in the same fashion with or without the next line commented
    //viewController.view;

    id mockWebView = [OCMockObject partialMockForObject:webView];
    // When I comment out the following line, the test passes
    [[mockWebView expect] loadRequest:[OCMArg any]];

    viewController.webView = mockWebView;

    [viewController viewDidLoad];
    [mockWebView verify];
    [mockWebView release];
}

推荐答案

事实证明,这是直到您看了几十遍后才注意到的一个字符问题.

This turned out to be one of those off by one character issues that you don't notice until you've looked at it a few dozen times.

此帖子在OCMock论坛上,我已将我的单元测试目标的其他链接器标志"设置为 -ObjC -forceload $(PROJECT_DIR)/Libraries/libOCMock.a .这是错误的; -forceload 应该是 -force_load .修正此错字后,我的测试就可以了.

Per this post on the OCMock forums, I had set my Other Linker Flags for my unit test target to -ObjC -forceload $(PROJECT_DIR)/Libraries/libOCMock.a. This is wrong; -forceload should have been -force_load. Once I fixed this typo, my tests worked.

这篇关于OCMock:为什么在尝试调用UIWebView模拟时出现无法识别的选择器异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆