erikdoe / ocmock

Mock objects for Objective-C
http://ocmock.org
Apache License 2.0
2.16k stars 606 forks source link

Mock [UIApplication sharedApplication] respondsToSelector response #315

Closed dogo closed 8 years ago

dogo commented 8 years ago

hi guys, I'm trying to mock the [[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)] response to NO.

What I'm doing wrong?

    it(@"should askForRegisteringRemoteNotificationForApplication for iOs 7.x or lower", ^{
        id mockApplication = OCMClassMock(UIApplication.class);
        OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
        OCMStub([[mockApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]).andReturn(@NO);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        OCMExpect([mockApplication registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
                                                                       UIRemoteNotificationTypeSound)]);
#pragma clang diagnostic pop        

        [DevicePushConfiguration askForRegisteringRemoteNotificationForApplication];

        OCMVerifyAll(mockApplication);
        [mockApplication stopMocking];
    });
+ (void)askForRegisteringRemoteNotificationForApplication {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
}

Thanks

erikdoe commented 8 years ago

Please see section 10.4 in the documentation. Certain core runtime methods can't be mocked. respondsToSelector: is one of them even though it's not explicitly listed.

dogo commented 8 years ago

@erikdoe Thanks

erikdoe commented 8 years ago

No worries. It's easy to overlook. I've tried to clarify the documentation now.