问题
I'm using OCMock and I'm trying to do to something like this in one of my tests:
[[mockScrollView expect] setContentSize:[OCMArg any]];
The problem is that [OCMArg any]
returns an id
type, and I want to use any CGSize
, because I don't know it's exact value. How can I pass this argument?
回答1:
With version 2.2 of OCMock you can use ignoringNonObjectArgs
[[mockScrollView expect] ignoringNonObjectArgs] setContentSize:dummySize];
回答2:
AFAIK there is no way to accomplish that with OCMock.
An alternative is to create a hand made mock. This is a subclass of UIScrollView
where you override setContentSize:
assigning the given size to a ivar that later on you can inspect.
Other easier option is to use a real UIScrollView
and check directly is the contentSize
is the one you expect. I would go for this solution.
回答3:
Sadly it looks like you'd have to extend OCMock in order to accomplish this. You could follow this pattern...
OCMArg.h
// Add this:
+ (CGSize)anyCGSize;
OCMArg.c
// Add this:
+ (CGSize)anyCGSize
{
return CGSizeMake(0.1245, 5.6789);
}
// Edit this method:
+ (id)resolveSpecialValues:(NSValue *)value
{
const char *type = [value objCType];
// Add this:
if(type[0] == '{')
{
NSString *typeString = [[[NSString alloc] initWithCString:type encoding:NSUTF8StringEncoding] autorelease];
if ([typeString rangeOfString:@"CGSize"].location != NSNotFound)
{
CGSize size = [value CGSizeValue];
if (CGSizeEqualToSize(size, CGSizeMake(0.1245, 5.6789)))
{
return [OCMArg any];
}
}
}
// Existing code stays the same...
if(type[0] == '^')
{
void *pointer = [value pointerValue];
if(pointer == (void *)0x01234567)
return [OCMArg any];
if((pointer != NULL) && (object_getClass((id)pointer) == [OCMPassByRefSetter class]))
return (id)pointer;
}
return value;
}
来源:https://stackoverflow.com/questions/16916115/ocmock-passing-any-cgsize