Bug mocking methods with char * arguments

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.

Bug mocking methods with char * arguments

Postby cooksey » 09 Feb 2013, 00:24

It looks like it's not possible to stub methods that take char * (or uint8_t *) arguments. The processing path in the matching code checks for '^' as a type encoding, which all pointers except for char * use. I've tried adding '*' as a type encoding to the same code path, but it hasn't worked yet.

It also looks to me like there might be a problem matching arguments with [OCMArg anyPointer]. OCMockRecorder is recording the invocation with the correct value, but when it comes time to match, the recorded argument has changed.

Specifically, I'm trying to stub a call to NSInputStream like

Code: Select all
id stream = [OCMockObject mockForClass:[NSInputStream class]];
[[[stream stub] andDo:^(NSInvocation *)invocation {
  // Do some stuff
}] read:[OCMArg anyPointer] maxLength:1024];

The length parameter matches the call I make later, but the pointer argument does not match, and the stubbed pointer that's reported does not match the [OCMArg anyPointer] special value.

Re: Bug mocking methods with char * arguments

Postby cooksey » 11 Feb 2013, 18:29

Looks like my patch is failing because -[NSInvocation retainArguments] causes the NSInvocation to copy C string arguments, so the special anyPointer value is being lost.

I've been able to work around this by replacing +[NSInputStream instanceMethodSignatureForSelector:] in a testing category, so that it returns the type encoding '^C' instead of '*'. Is there a better way to handle this?

Return to OCMock