Skip to content

Instantly share code, notes, and snippets.

@esmasui
Created May 2, 2012 18:22
Show Gist options
  • Save esmasui/2578933 to your computer and use it in GitHub Desktop.
Save esmasui/2578933 to your computer and use it in GitHub Desktop.
#import "SwizzlingTest.h"
#import </usr/include/objc/objc-class.h>
@interface Swizzlingee : NSObject
- (NSString *) hoge;
- (NSString *) piyo;
@end
@implementation Swizzlingee
- (NSString *)hoge
{
static NSString *s = @"hoge";
return s;
}
- (NSString *)piyo
{
static NSString *s = @"piyo";
return s;
}
@end
@implementation SwizzlingTest
- (void)testThatSwizzled
{
//Do swizzle
Method hogeMethod = class_getInstanceMethod([Swizzlingee class], @selector(hoge));
Method piyoMethod = class_getInstanceMethod([Swizzlingee class], @selector(piyo));
method_exchangeImplementations(hogeMethod, piyoMethod);
//Invoke the method
Swizzlingee *underTest = [[Swizzlingee alloc] init];
NSString *s = [underTest hoge];
STAssertEqualObjects(s, @"piyo", @"hoge method should returns piyo after swizzled");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment