ext*_*dom 7 tdd objective-c ocmock ios
我正在使用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)];
Run Code Online (Sandbox Code Playgroud)
我也遇到了这个限制。基本思想是重写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];
Run Code Online (Sandbox Code Playgroud)
如果您不打电话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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2017 次 |
最近记录: |