I ran into the same problem trying to simulate a click on a table cell in order to automate a test for a view controller that handles clicking on a table.
The controller has its own UITapGestureRecognizer created as shown below:
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapOnTableView)];
unit test should simulate a touch so that gestureRecognizer initiates an action because it was created from user interaction.
None of the proposed solutions worked in this scenario, so I decided that it decorates the UITapGestureRecognizer by creating precise methods called the controller. Therefore, I added a "performTap" method that calls the action in such a way that the controller itself does not know where this is coming from. That way, I could make a test block for the controller, regardless of the gesture recognizer, just the called action.
This is my category, hope this helps someone.
CGPoint mockTappedPoint; UIView *mockTappedView = nil; id mockTarget = nil; SEL mockAction; @implementation UITapGestureRecognizer (MockedGesture) -(id)initWithTarget:(id)target action:(SEL)action { mockTarget = target; mockAction = action; return [super initWithTarget:target action:action];
Glauco Aquino Apr 13 '13 at 4:39
source share