I hit this limitation too. The main idea is to override respondsToSelector : (which CANNOT be reliably ridiculed by OCMock).
I made the following class that does this for you. Then you can use it as follows:
extend GCOCMockOptionalMethodSupportingObject and implement your protocol
@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];
If you do not call markSelectorAsImplemented , then your classBeingTested will receive NO for respondsToSleectorForThatMethod
I posted the code here. I use this for a great effect. Thanks to jer at #iphonedev for disabling me along the way (overriding respondsToSelector was his idea, I did some crazy runtime methods - this is a much cleaner concept).
here is the code
@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 { }
Infrid
source share