Skip to content

Method swizzling, change the implementation of an existing selector.

License

Notifications You must be signed in to change notification settings

remirobert/RROLoadSwizzle

Repository files navigation

RROLoadSwizzle

buid status codecov

Used to assist in unit testing. Check Tests for usage.

//A function which is the implementation of original method. 
//The function must take at least two arguments—self and _cmd.
void (*gOriginalViewDidLoad)(id, SEL);

- (void)tearDown {
    RRO_REVERT_SWIZZLE_ALL_METHOD([UIViewController class]);
}

- (void)testThatViewDidLoadIsCalledWhenShowWindow {
  __block BOOL isViewDidLoadCalled = NO;
  
  void (^viewDidLoadBlock)(UIViewController *, SEL) = ^void (UIViewController *nav, SEL _cmd) {
    isViewDidLoadCalled = true;
    NSLog(@"viewDidLoad called here");

    //call original @selector(viewDidLoad) if needed
    gOriginalViewDidLoad(nav, _cmd); 
  };
  
  RRO_SWIZZLE_BLOCK([UIViewController class], @selector(viewDidLoad), viewDidLoadBlock, &gOriginalViewDidLoad);
  
  UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  window.rootViewController = [[UIViewController alloc] init];
  [window makeKeyAndVisible];
  
  XCTAssertTrue(isViewDidLoadCalled);
}

About

Method swizzling, change the implementation of an existing selector.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published