I am currently trying to test some code that uses AVAudioSession
and my attempts to mock it since it's a singleton have proved difficult thus far, and i did a bit of research and came across the idea to swizzle the way it get's it's instance to then actually initialise your subclass as you would want it to, but i am having trouble figuring out what methods to swizzle. I tried sharedInstance and the class_addMethod()
returns yes for saying it's added rather than replacing it. Can I effectively mock a singleton this way?
@interface AVAudioSessionFake : AVAudioSession
@property (readonly, nonatomic) BOOL wasSetActiveErrorCalled;
-(instancetype)initStub;
@end
@implementation AVAudioSessionFake
+ (void)load
{
[AVAudioSessionFake swizzleOriginalMethod:@"sharedInstance" with:@"initStub"];
}
+ (void)swizzleOriginalMethod:(NSString*)Original with:(NSString*)replacement
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
Class class = [self class];
SEL originalSelector = NSSelectorFromString(Original);
SEL swizzledSelector = NSSelectorFromString(replacement);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod)
{
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}
else
{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
-(instancetype)initStub
{
return [[[self class]alloc]init];
}
- (BOOL)setActive:(BOOL)active error:(NSError *__autoreleasing *)outError
{
_wasSetActiveErrorCalled = YES;
return [super setActive:active error:outError];
}
@end