jonreid/MockUIAlertController

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Closed this issue · 3 comments

When running a test that shows an UIAlertController, I get:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7fb245490b40>)

I know it's probably not an issue of MockUIAlertController (I was mocking myself before and getting the same error), but I wanted to know what would be your approach.

Here's the relevant code:

UIViewController *viewController = [UIViewController new];
NSURL *URL = [NSURL URLWithString:@"https://www.apple.com"];

QCOMockAlertVerifier *alertVerifier = [[QCOMockAlertVerifier alloc] init];

WFLExternalLinkPresenter *presenter = [WFLExternalLinkPresenterWithoutSafari new];
[presenter openURL:URL withTitle:@"Apple" presentingViewController:viewController];

expect(alertVerifier.title).to.equal(@"Apple");

Any thoughts?

Forcing the view to load when presenting fixes this:

- (void)qcoMock_presentViewController:(UIViewController *)viewControllerToPresent
                             animated:(BOOL)flag
                           completion:(void (^)(void))completion
{
    __unused viewControllerToPresent.view;
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:QCOMockAlertControllerPresentedNotification
                      object:viewControllerToPresent
                    userInfo:@{ QCOMockViewControllerAnimatedKey : @(flag) }];
    if (completion)
        completion();
}

Ahhh.

Thank you!