Skip to content

@pk /ExampleTest.m
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Using method swizzling and blocks to test Class methods in Objective-C.
#import "SenTestCase+MethodSwizzling.m"
@interface ExampleTest : SenTestCase {}
+ (BOOL)trueMethod;
+ (BOOL)falseMethod;
@end
@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]
executeBlock:^{
assertThatBool([[self class] trueMethod], isEqualToBool(NO));
}]
}
@end
#include <objc/runtime.h>
@interface SenTestCase (MethodSwizzling)
- (void)swizzleMethod:(SEL)aOriginalMethod
inClass:(Class)aOriginalClass
withMethod:(SEL)aNewMethod
fromClass:(Class)aNewClass
executeBlock:(void (^)(void))aBlock;
@end
@implementation SenTestCase (MethodSwizzling)
- (void)swizzleMethod:(SEL)aOriginalMethod
inClass:(Class)aOriginalClass
withMethod:(SEL)aNewMethod
fromClass:(Class)aNewClass
executeBlock:(void (^)(void))aBlock {
Method originalMethod = class_getClassMethod(aOriginalClass, aOriginalMethod);
Method mockMethod = class_getInstanceMethod(aNewClass, aNewMethod);
method_exchangeImplementations(originalMethod, mockMethod);
aBlock();
method_exchangeImplementations(mockMethod, originalMethod);
}
@end
@mattdipasquale

Can you show an example of how to use this?

@pk
Owner
pk commented

Hi,

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]
           executeBlock:test];
@mattdipasquale

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
Owner
pk commented

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

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.

@letsdev

Thx reborg, that helped a lot!

@nheinric

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

@felixhammerl

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
Something went wrong with that request. Please try again.