OCMock passing any CGSize

给你一囗甜甜゛ 提交于 2019-12-11 13:15:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!