问题
Edit: This was all caused by a typo in my Other Link Flags setting. See my answer below for more information.
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
My test code is:
- (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.
Any ideas? Is mocking of UIKit objects supported by OCMock?
Edit: 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];
}
回答1:
UIKit classes are mysterious beasts, and I've found that mucking around with mocking them can lead to hours of debugging fun. That said, I've found that with a little patience you can make it work.
The first thing I notice with your code is that your controller doesn't load its view in your test. I generally make sure to always force the view to load before any tests run. That, of course, means you can't write expectations for the initialization of your web view, but in this case you don't really need to. You could do this:
- (void)testViewDidLoadSetsWebViewDelegateToSelf {
MyViewController *viewController = [[MyViewController alloc] init];
// Force the view to load
viewController.view;
assertThat(controller.webView.delegate, equalTo(controller));
}
That said, if you do want to then subsequently mock the web view, I would recommend using a partial mock for the existing web view:
- (void)testWebViewDoesSomething {
MyViewController *viewController = [[MyViewController alloc] init];
// Force the view to load
viewController.view;
id mockWebView = [OCMockObject partialMockForObject:controller.webView];
[[mockWebView expect] someMethod];
[controller doWhatever];
[mockWebView verify];
}
In fact, I've found it's best to always use a partial mock for any UIView subclass. If you create a full mock of a UIView it will almost always blow up messily when you try to do something view-related with it, such as add it to a superview.
回答2:
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.
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.
来源:https://stackoverflow.com/questions/4155690/ocmock-why-do-i-get-an-unrecognized-selector-exception-when-attempting-to-call