Question

I am new to OCMock and I needed some help getting started. I have a UIScrollView that upon panning triggers an event handler which then does some stuff. I'd like to test this. Here's how I make the object

id gestureMock = [OCMockObject partialMockForObject:[UIPanGestureRecognizer new]];
  1. Now how do I set the panning specifications?

  2. After initializing the panning, how do I "invoke" the pan?

Was it helpful?

Solution

If you are testing that your code responds properly to the UIPanGestureRecognizer, write the test around the target method of the gesture recognizer.

// If you have this...
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
[self.scrollView addGestureRecognizer:pgr];

// With this...
- (void)didPan:(UIPanGestureRecognizer *)pgr
{
    switch (pgr.state):
    {
        case UIGestureRecognizerStateEnded:
            [self doSomething];
            break;
        default:
    }
}

// Then you could do this
id gestureMock = [OCMockObject mockForClass:[UIPanGestureRecognizer class]];
[[[gestureMock stub] andReturn:UIPanGestureRecognizerStateEnded] state];
id objectUnderTest = [OCMockObject partialMockForObject:[MyClass new]];
[[objectUnderTest expect] doSomething];
// Make the call
[objectUnderTest didPan:gestureMock];
[objectUnderTest verify];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top