iOS OCMock:如何测试平移

时间:2013-08-09 19:20:25

标签: ios uipangesturerecognizer ocmock

我是OCMock的新手,我需要一些帮助才能开始。我有一个UIScrollView,在平移时触发一个事件处理程序,然后执行一些操作。我想测试一下。这是我如何制作对象

id gestureMock = [OCMockObject partialMockForObject:[UIPanGestureRecognizer new]];
  1. 现在如何设置平移规格?

  2. 初始化平移后,如何“调用”平底锅?

1 个答案:

答案 0 :(得分:0)

如果您正在测试您的代码是否正确响应UIPanGestureRecognizer,请围绕手势识别器的目标方法编写测试。

// 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];