iosobjective-cunit-testingocmock

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?


Solution

  • ok, i think it figured it out. as i suspected, it was a simple noob mistake. changing [OCMArg anyPointer] to [OCMArg any] makes the following work:

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