public
Last active

  • Download Gist
GHTestCase+Swizzle.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11
@class GHTestCase;
 
@interface GHTestCase (Swizzle)
 
+ (id)sharedMock;
+ (void)setSharedMock:(id)newMock;
 
- (void)swizzle:(Class)target_class selector:(SEL)selector;
- (void)deswizzle;
 
@end
GHTestCase+Swizzle.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#import "GHUnit.h"
#import "GHTestCase+Swizzle.h"
#import <objc/objc-runtime.h>
 
id sharedMockPointer = nil;
 
@implementation GHTestCase (Swizzle)
 
+(id)sharedMock
{
return sharedMockPointer;
}
 
+(void)setSharedMock:(id)newMock
{
sharedMockPointer = newMock;
}
 
Method originalMethod = nil;
Method swizzleMethod = nil;
 
- (void)swizzle:(Class)target_class selector:(SEL)selector
{
originalMethod = class_getClassMethod(target_class, selector);
swizzleMethod = class_getInstanceMethod([self class], selector);
method_exchangeImplementations(originalMethod, swizzleMethod);
}
 
- (void)deswizzle
{
method_exchangeImplementations(swizzleMethod, originalMethod);
swizzleMethod = nil;
originalMethod = nil;
}
 
@end

Hi I found your gist while searching for testing of the Class methods, it inspired me to implement the same concepts with blocks: git://gist.github.com/1038034.git

Thanks for sharing!

That is awesome! Thanks for the update with blocks.

Fantastic.

I had trouble compiling this for the simulator (Xcode 4.2).

If you change

#import <objc/objc-runtime.h 

to

#import <objc/runtime.h>

this will compile and run in both the simulator and devices.

Sorry for the multiple edits - goofed on the formatting.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.