3

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
Genhain
  • 1,937
  • 1
  • 19
  • 38
  • You want to stub `sharedInstance` method to return your `AVAudioSessionFake`, right ? – Huy Nghia Apr 17 '15 at 06:20
  • Not ultimately just as long as i can create an instance of `AVAudioSessionFake` while being a subclass of `AVAudioSession` so i can test that certain methods get called when they should. if stubbing sharedInstance achieves that sure that's what i want. – Genhain Apr 20 '15 at 02:57

1 Answers1

0

You could swizzle sharedInstance method to return your AVAudioSessionFake like this

#AVAudioSessionFake:

@interface AVAudioSessionFake : AVAudioSession
@property (readonly,    nonatomic) BOOL wasSetActiveErrorCalled;

@end

@implementation AVAudioSessionFake
-(instancetype)init
{
    if (self == [super init]) {
        // your init code
    }
    return self;
}
 -(BOOL)setActive:(BOOL)active error:(NSError *__autoreleasing *)outError
{
     _wasSetActiveErrorCalled = YES;

    return [super setActive:active error:outError];
}
@end

# swizzle sharedInstance:

static Method original_AVAudioSession_SharedInstance_ClassMethod;
static Method swizzled_AVAudioSession_SharedInstance_ClassMethod;
@interface AVAudioSession (AVAudioSessionSwizzledSharedInstance) 
@end
@implementation AVAudioSession (AVAudioSessionSwizzledSharedInstance)
    + (void)load
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^
        {
          Class class = object_getClass((id)self);
          SEL original_Selector = @selector(sharedInstance);
          SEL swizzled_Selector = @selector(swizzled_sharedInstance);
          original_AVAudioSession_SharedInstance_ClassMethod = class_getInstanceMethod(class, original_Selector);
          swizzled_AVAudioSession_SharedInstance_ClassMethod = class_getInstanceMethod(class, swizzled_Selector);
        });
    }
    + (void)swizzling_AVAudioSession_SharedInstance
    {    
    method_exchangeImplementations(original_AVAudioSession_SharedInstance_ClassMethod,swizzled_AVAudioSession_SharedInstance_ClassMethod);
    }
    + (id)swizzled_sharedInstance 
    {
         static dispatch_once_t p = 0;
        // initialize sharedObject as nil (first call only)
        __strong static AVAudioSessionFake _sharedObject = nil;

        dispatch_once(&p, ^{
            _sharedObject = [[AVAudioSessionFake alloc] init];
        });
        return _sharedObject;
    }

# Use:

- (void)testAnAVAudioSessionMethod {
   [AVAudioSession swizzling_AVAudioSession_SharedInstance]; //Start swizzling shareInstance method
   // some test code and assert result here
   [AVAudioSession swizzling_AVAudioSession_SharedInstance]; //return original method
}

Some helpful link The Right Way to Swizzle in Objective-C, method swizzling, another question in SO

Community
  • 1
  • 1
Huy Nghia
  • 996
  • 8
  • 22
  • Hey Sorry for the late reply and thank you for taking the time. I eventually decided to just trust that apple knows what it is doing and leave testing of certain called methods here. However i did attempt your solution and it did not work for me i am afraid... It still returned an `+ (id)swizzled_sharedInstance` still returned an AVAudioSession rather than the fake – Genhain Apr 27 '15 at 03:09