Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCMock method name collision

i'm a new user of OCMock, so maybe i'm just missing something simple here. this code does not compile:

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
[[mockSession expect]  addOutput:[OCMArg anyPointer]];

the error is

Multiple methods named 'addOutput:' found with mismatched result, parameter type or attributes

the signature of the method addOutput on AVCaptureSession is as follows

- (void)addOutput:(AVCaptureOutput *)output

as far as i can tell, the problem is that the method addOutput exists on both the AVCaptureSession and AVAssetReader classes. the method signature for addOutput on AVAssetReader is as follows.

- (void)addOutput:(AVAssetReaderOutput *)output

apparently the compiler thinks my mockSession is an AVAssetReader, but i don't know why it chooses that class instead of AVCaptureSession. if i expect a different method on AVCaptureSession that does not exist on AVAssetReader, then it compiles. i have tried the following without success. it compiles, but crashes.

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
[(AVCaptureSession*)[mockSession expect]  addOutput:[OCMArg anyPointer]];

this code also does not compile, with the same error as the previous one

id mockSession = [OCMockObject mockForClass:[AVCaptureSession class]];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[[mockSession expect]  addOutput:output];

any guidance here?

like image 766
Ben H Avatar asked Jan 25 '13 19:01

Ben H


1 Answers

In cases where your variable is an "id" but a method is declared with different signatures in different classes you should help the compiler by casting the object to the correct type, e.g.

[((AVCaptureSession *)[mockSession expect])  addOutput:[OCMArg any]];

In either case, if the argument is an object, as it seems in your case, you should use any and not anyPointer. But you figured that one out already. ;-)

like image 108
Erik Doernenburg Avatar answered Nov 15 '22 12:11

Erik Doernenburg