7

我正在使用 OCMock 在我的 iOS 应用程序测试中创建模拟,并且我想创建不实现所有可选方法的协议模拟。

如果不清楚我的意思......这里有一些代码:

// Protocol definition
@protocol MyAwesomeProtocol
    - (void)doThatRequiredThing;
    @optional
    - (void)doThatOptionalThing;
@end

...

// In a test
id mock = [OCMockObject mockObjectForProtocol:@protocol(MyAwesomeProtocol)];

// This should return YES:
[mock respondsToSelector:@selector(doThatRequiredThing)];
// This should return NO:
[mock respondsToSelector:@selector(doThatOptionalThing)];
4

2 回答 2

1

我也达到了这个限制。基本思想是覆盖respondsToSelector: (OCMock 不能可靠地模拟它)。

我做了以下课程,为你做这件事。然后,您可以按如下方式使用它:

扩展GCOCMockOptionalMethodSupportingObject并实现你的协议

@interface GCTestDelegate : GCOCMockOptionalMethodSupportingObject <GCDelegate>
@end

@implementation GCTestDelegate

//required methods
- (void)requiredMethod{
}
@end

// create your testdelegate
self.classBeingTested.delegate =  [OCMock partialMockForObject:[GCTestDelegate new]];
[self.classBeingTested.delegate markSelectorAsImplemented:@selector(optionalMethod:)];
[[self.classBeingTested.delegate expect] optionalMethod:self.classBeingTested];
[self.classBeingTested doSomethingThatwillCheckIfYourDelegateRespondsToYourOptionalMethod];

如果你不打电话markSelectorAsImplemented,那么你classBeingTested将得到 NOrespondsToSleectorForThatMethod

我已经把它的代码放在这里了。我用这个效果很好。感谢 #iphonedev 上的 jer 让我走上了这条道路(压倒一切respondsToSelector的是他的想法,我正在做一些疯狂的运行时方法添加 - 这更干净我认为)。

这是代码

/**
 * This class is specifically useful and intended for testing code paths that branch
 * pending implementation of optional methods.
 * OCMock does not support mocking of protocols with unimplemented optional methods.
 * Further compounding the issue is the fact that OCMock does not allow mocking of
 * respondsToSelector (in fact, it does but the behaviour is undefined),
 * As such this class can be extending to implement a given protocol, the methods can be mocked/expected
 * as normal, but in addition we can tell the class to report it conforms to a protocol method or not.
 *
*/
@interface GCOCMockOptionalMethodSupportingObject : NSObject

- (void)markSelectorAsImplemented:(SEL)aSelector;
- (void)unmarkSelectorAsImplemented:(SEL)aSelector;


@end

#import "GCOCMockOptionalMethodSupportingObject.h"


@interface GCOCMockOptionalMethodSupportingObject ()
@property(nonatomic, strong) NSMutableArray *implementedSelectors;

@end

@implementation GCOCMockOptionalMethodSupportingObject {

}
//////////////////////////////////////////////////////////////
#pragma mark init 
//////////////////////////////////////////////////////////////

- (id)init {
    self = [super init];
    if (self) {
        self.implementedSelectors = [NSMutableArray array];
    }

    return self;
}

//////////////////////////////////////////////////////////////
#pragma mark public api
//////////////////////////////////////////////////////////////


- (void)markSelectorAsImplemented:(SEL)aSelector {
    if (![self isImplemented:aSelector]) {
        [self.implementedSelectors addObject:NSStringFromSelector(aSelector)];
    }
}


- (void)unmarkSelectorAsImplemented:(SEL)aSelector {
    for (NSString *selectorValue in [self.implementedSelectors mutableCopy]) {
        SEL storedSelector = NSSelectorFromString(selectorValue);
        if (sel_isEqual(aSelector, storedSelector)) {
            [self.implementedSelectors removeObject:selectorValue];
            break;
        }
    }
}


//////////////////////////////////////////////////////////////
#pragma mark private impl
//////////////////////////////////////////////////////////////


- (BOOL)isImplemented:(SEL)aSelector {
    for (NSString *selectorValue in self.implementedSelectors) {
        SEL storedSelector = NSSelectorFromString(selectorValue);
        if (sel_isEqual(aSelector, storedSelector)) {
            return YES;
        }
    }
    return NO;
}

//////////////////////////////////////////////////////////////
#pragma mark overridden
//////////////////////////////////////////////////////////////

- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self isImplemented:aSelector]) {
        return YES;
    } else {
        return [super respondsToSelector:aSelector];
    }
}

@end
于 2012-10-22T15:29:17.200 回答
1

最简单的做法是创建一个包含您想要实现的选择器的类。不需要任何实现。然后,您创建该类的类模拟而不是协议模拟,并以相同的方式使用它。

例如:

@interface MyAwesomeImplementation : NSObject <MyAwesomeProtocol>
- (void)doThatRequiredThing;
@end
@implementation MyAwesomeImplementation
- (void)doThatRequiredThing {}
@end

id mock = OCMStrictClassMock([MyAwesomeImplementation class]);
于 2016-04-29T16:09:02.580 回答