mardi 2 juin 2015

How to unit test class method in ios

I'm struggling to understand unit testing in ios and I need to test the following method. Here is interface of the class and implementation of the method I need to test.

extern NSString * const ObtainFinished;
extern NSString * const ObtainFailed;

@interface MyObjectsController : NSObject

// api
+ (void)obtainObjects;

// data
+ (BOOL)isObtaining;
+ (NSArray *)objects;

@end

The implementation of +(void) obtainObjects method is the following:

+ (void)obtainObjects {
    [[self sharedInstance] obtainObjects];
}

- (void)obtainObjects {
    if (self.isObtaining) {
        return;
    }

    self.isObtaining = YES;
    [NOTIFICATION_CENTER postNotificationName:ObtainStarted
                                       object:self];

    MyRequestSuccessBlock successBlock = ^(id response, NSDictionary *responseHeaders, MyResponseObject *responseObject) {
        self.isObtaining = NO;
        NSArray *array = responseObject.responseData;
        NSArray *objects;
        if ([array isKindOfClass:[NSArray class]]) {
            objects = [EKMapper arrayOfObjectsFromExternalRepresentation:array withMapping:[MyObject objectMapping]];
        }
        self.objects = objects;

        [NOTIFICATION_CENTER postNotificationName:ObtainFinished
                                           object:self];
    };

    MyRequestFailureBlock failureBlock = ^(id response, NSError *error, MyClientServerResponseObject *responseObject) {
        self.isObtaining = NO;         
        [NOTIFICATION_CENTER postNotificationName:ObtainFailed
                                           object:self];
    };

    [MyClientServerManager addGetRequestForPath: WR_API_ACHIEVEMENTS_PATH
                                     parameters:nil
                                        headers:nil
                                           body:nil
                                   successBlock:successBlock
                                   failureBlock:failureBlock];
}

How do I need to refactor this method? How can I test that proper notifications are posted? Maybe there is some other functionality need to be tested?

Aucun commentaire:

Enregistrer un commentaire