1

I am using a UIAlertView+Blocks with a tapBlock from https://github.com/ryanmaxwell/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];

I can mock the alert view with OCMock and test that it has popped up.

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

But I'd also want to delegate the tapBlock to its original implementation. Sadly it doesn't look like I can make a partial mock here.

How can I solve that? In the end I want to test that the popup appears and if the user clicks on the right button things that are inside of that block happen.

dB.
  • 4,700
  • 2
  • 46
  • 51
  • Does this answer help? http://stackoverflow.com/a/20663781/449161 – Ben Flynn Jan 17 '14 at 19:39
  • @BenFlynn I tried saying "on show i can just simulate the tap" with [[[mockResetPasswordView expect] andDo:^(NSInvocation *invocation) { void (^handler)(UIAlertView *, NSInteger); handler = mockResetPasswordView.tapBlock; handler(mockResetPasswordView, 0); }] show]; But that means I don't actually have an alert, how do I tap on a button? I can of course just call whatever I want here, but doesn't look like I can get the original implementation of by tapBlock. – dB. Jan 19 '14 at 17:06
  • I also ran into http://stackoverflow.com/questions/21219938/why-doesnt-ocmock-see-class-methods-in-extensions when trying @BenFlynn's suggestion, related. – dB. Jan 19 '14 at 17:16
  • Sorry for the delay of response. Isn't it sufficient to manually call the actions that would occur if the user tapped the button (in this case the handler block)? I don't think you'd need to worry about the tapping unless you were testing the implementation of the alert itself. – Ben Flynn Jan 27 '14 at 16:31

0 Answers0