Unrecognized selector when attempting to call UIWebView mock

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Unrecognized selector when attempting to call UIWebView mock

Postby gjritter » 11 Nov 2010, 16:44

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 getting the following error when the view controller attempts to call the expected method.

Code: Select all
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:

Code: Select all
- (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:


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


I'm very new to OCMock (started using it yesterday), but familiar with various mock object frameworks in other languages. I have written other tests with OCMock that are running successfully, so I'm not sure what I'm doing wrong with this test.

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


Code: Select all
- (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 above, the error returned when using the partial mock.

Any ideas? Is mocking of UIKit objects supported by OCMock?
gjritter
 
Posts: 3
Joined: 11 Nov 2010, 16:22

Re: Unrecognized selector when attempting to call UIWebView mock

Postby gjritter » 16 Nov 2010, 04:34

FYI, I've also posted this question at Stack Overflow, but so far I haven't found a solution. If anyone has any ideas, please let me know.

The stack overflow post is at: http://stackoverflow.com/questions/4155690/ocmock-why-do-i-get-an-unrecognized-selector-exception-when-attempting-to-call-a
gjritter
 
Posts: 3
Joined: 11 Nov 2010, 16:22

Re: Unrecognized selector when attempting to call UIWebView mock

Postby gjritter » 16 Nov 2010, 18:17

I had a typo in my Other Linker Flags setting of my unit testing target. I had them set to include:

Code: Select all
-ObjC -forceload $(PROJECT_DIR)/Libraries/libOCMock.a


The -forceload is incorrect; it should have been -force_load. I'm not sure how many times I looked at those settings over the last few days without noticing the problem. :oops:
gjritter
 
Posts: 3
Joined: 11 Nov 2010, 16:22

Re: Unrecognized selector when attempting to call UIWebView mock

Postby erik » 28 Jan 2011, 20:43

This seems to be a common issue. I've now added a small check to the initialiser of OCMockObject that raises an exception with a message describing the problem if the category methods aren't present.

erik
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany


Return to OCMock



cron