如何使用回调模拟UIAlertView?

时间:2014-01-16 23:11:42

标签: uialertview ocmock

我正在使用来自https://github.com/ryanmaxwell/UIAlertView-Blocks的tapBlock的UIAlertView + Blocks。

UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"Title"
    message:@"Please press a button."
    delegate:nil
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tapBlock = ^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == alertView.firstOtherButtonIndex) {
            // things happen here
        }
    };
    [alert show];

我可以使用OCMock模拟警报视图并测试它是否已弹出。

 id mockAlertView = [OCMockObject mockForClass:[UIAlertView class]];
 [[[mockAlertView stub] andReturn:mockAlertView] alloc];
 (void)[[[mockAlertView expect] andReturn:mockAlertView]
                            initWithTitle:@"Title"
                                  message:@"Please press a button."
                                 delegate:OCMOCK_ANY
                        cancelButtonTitle:OCMOCK_ANY
                        otherButtonTitles:OCMOCK_ANY, nil];
 [[mockAlertView expect] setTapBlock:OCMOCK_ANY]; // this is not what I want
 [[mockAlertView expect] show];

 // cause the alert view to show

 [mockAlertView verify];
 [mockAlertView stopMocking];

但是我也想将tapBlock委托给它原来的实现。可悲的是,看起来我不能在这里做出局部模拟。

我该如何解决?最后,我想测试弹出窗口是否出现,如果用户点击右键,则会发生该块内部的事情。

0 个答案:

没有答案