Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Using method swizzling and blocks to test Class methods in Objective-C.
#import "SenTestCase+MethodSwizzling.m"
@interface ExampleTest : SenTestCase {}
+ (BOOL)trueMethod;
+ (BOOL)falseMethod;
@implementation ExampleTest
+ (BOOL)trueMethod { return YES; }
+ (BOOL)falseMethod { return NO; }
- (void)testMethodSwizzlingShouldCallAlternativeImplementation {
[self swizzleMethod:@selector(trueMethod) inClass:[self class]
withMethod:@selector(falseMethod) fromClass:[self class]
assertThatBool([[self class] trueMethod], isEqualToBool(NO));
#include <objc/runtime.h>
@interface SenTestCase (MethodSwizzling)
- (void)swizzleMethod:(SEL)aOriginalMethod
executeBlock:(void (^)(void))aBlock;
@implementation SenTestCase (MethodSwizzling)
- (void)swizzleMethod:(SEL)aOriginalMethod
executeBlock:(void (^)(void))aBlock {
Method originalMethod = class_getClassMethod(aOriginalClass, aOriginalMethod);
Method mockMethod = class_getInstanceMethod(aNewClass, aNewMethod);
method_exchangeImplementations(originalMethod, mockMethod);
method_exchangeImplementations(mockMethod, originalMethod);

Can you show an example of how to use this?


pk commented Sep 22, 2011


I use that for swizzling class methods. You might need that in tests for example when you want to mock class method:

    void *test = ^{ assertThatBool([mock needsUpdate], equalToBool(YES)); };
    [self swizzleMethod:@selector(applicationVersion)     inClass:[NXREnvironment class]
             withMethod:@selector(applicationVersion_200) fromClass:[self class]

Thanks. That helps a bit, but I'm still unsure how to use this. Can you show the code for needsUpdate, applicationVersion and applicationVersion_200? Also, please show where & how you stub the class method. Thanks.


pk commented Sep 22, 2011

Hi Matt,

is this more general question about the Method Swizzling in ObjC? The gist is just method swizzling implementation done using blocks so you don't need to swizzle methods back and forth. I explain it without code of the methods you ask for as you only need to rely on interface in this case.

What the example code posted does is:

I have a method needsUpdate which returns BOOL, block just validate that. It's not important what/how needsUpdates compute it's value, the only important bit is - it at some point use class method NXREnvironment::applicationVersion.

It is not easy to mock class methods in ObjectiveC so it's usually solved by swizzling - substituting - implementation of the method.

So to mock class method NXREnvironment::applicationVersion I define my special method with known return value on the test class (self) which is applicationVersion_200 - it just returns NSString 2.0.0.

Using swizzleMethod I swap implementation of the applicationVersion with implementation of my method applicationVersion_200. So in code of needsUpdate where I call applicationVersion, swizzled method implementation of applicationVersion_200 is called instead.

So as you see, we swapped implementation of method keeping code the same and signatures the same as well.

reborg commented Dec 17, 2011

Hi there,

cool trick, thanks for sharing. The example given should not work unless you extract the target method as
Method mockMethod = class_getClassMethod(aNewClass, aNewMethod);
If it's working then there is some trouble going on with equality of BOOLs. To test it, try to
[NSException raise:@"DEBUG" format:@"##### %@", mockMethod];
right after line 18. It should be null.


ghost commented Feb 9, 2012

Thx reborg, that helped a lot!

nheinric commented Jul 2, 2012

As a newbie to objc, this gist is really going to make my life easier. Thanks!

the problem with this is if the test case you put into the block fails, you will not "deswizzle" the method. this is likely to mess up your other test cases. that's why i prefer to do the swizzling and cleanup stuff in the -setUp and -tearDown...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment