OCMock passing any CGSize
Asked Answered
E

3

1

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?

Eleanoraeleanore answered 4/6, 2013 at 11:0 Comment(1)
This is a problem with OCMock. That said, mocking a view shouldn't generally be necessary. What are you trying to achieve?Eightieth
V
5

With version 2.2 of OCMock you can use ignoringNonObjectArgs

[[mockScrollView expect] ignoringNonObjectArgs] setContentSize:dummySize];
Visigoth answered 4/7, 2013 at 20:26 Comment(0)
G
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.

Gocart answered 4/6, 2013 at 16:55 Comment(1)
I agree that checking the result of the change in content size is a good way to go here.Maraca
M
1

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;
}
Maraca answered 4/6, 2013 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.