Search code examples
objective-cocmock

How can I mock an NSZone?


OCMock doesn't currently support loose matching of primitive arguments. Yet I have a property that I need to set on an OCMockObject, and this property is declared with the copy attribute. It therefore implements - (id)copyWithZone:(NSZone *)zone. NSZone is defined as typedef struct _NSZone NSZone, and as it's a struct, it's a primitive type. Hence I get the error

Incompatible pointer types sending 'id' to parameter of type 'NSZone *' (aka 'struct _NSZone *')

on this line:

[[mockObject expect] copyWithZone:[OCMArg any]];

Is the best way to resolve this declaring the property with the retain attribute rather than copy? The benefit of using copy is that the object has no possibility of getting modified by another object.


Solution

  • NSZone* is a pointer. While OCMock doesn't provide "loose" matching for primitive types it does have [OCMArg anyPointer] to match, well, any pointer.

    That said, if you just call expect, and not also andReturn:, then the mocked method will just return nil, which may not be what you want. My guess is the following is a better solution in your case:

    [[[mock expect] andReturn:mock] copyWithZone:[OCMArg anyPointer]];
    

    This obviously doesn't create a copy of the mock, it simply ensures that the mock itself is returned from the invocation of copyWithZone:.