mercredi 8 juillet 2015

Calling a private method with non-nil argument excepts despite the method succeeding

I'm writing unit tests for a class I wrote. To test that it posts notifications in a way I expect, I invoke a private method on the class externally. This works fine if I pass nil in, but if I pass any non-nil argument to it, I get an NSInvalidArgumentException that says an unrecognized selector was sent to an instance. I tried using an NSInvocation object as well and got the same results. I'm a bit confused as to what's going on, so I thought I'd share this and see if anyone else had a similar experience.

What could be causing an unrecognized selector to be sent to this instance? More confusing is the fact that I can verify the code calls postNotificationName:object:userInfo: but the test fails once myPrivateMethod returns to the calling unit test method.

Unit Test Code:

ClassToTest * _class = [[ClassToTest alloc] init];

NSArray * testAddresses = @[@"testAddress"];
NSDictionary * info = @{kAddresses: testAddresses};

expect(^{

    [_class performSelector: @selector(myPrivateMethod:) withObject: info];

}).to.postNotification(kNotificationName);

Private Method Implementation:

- (void) myPrivateMethod: (NSDictionary *) info {

    NSArray * addresses = info[kAddresses];

    for (NSString * address in addresses) {

        [[NSNotificationCenter defaultCenter] postNotificationName: kNotificationName
                                                        object: self
                                                      userInfo: @{kSomeValue: [ObjectFactory objectForAddress: address]}];

    }

}

Aucun commentaire:

Enregistrer un commentaire