How to test a class that makes HTTP request and parse the response data in Obj-C?

Posted by GuidoMB on Stack Overflow See other posts from Stack Overflow or by GuidoMB
Published on 2010-03-21T19:57:19Z Indexed on 2010/03/21 20:01 UTC
Read the original article Hit count: 427

I Have a Class that needs to make an HTTP request to a server in order to get some information. For example:

- (NSUInteger)newsCount {
    NSHTTPURLResponse *response;
    NSError *error;
    NSURLRequest *request = ISKBuildRequestWithURL(ISKDesktopURL, ISKGet, cookie, nil, nil);
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (!data) {
        NSLog(@"The user's(%@) news count could not be obtained:%@", username, [error description]);
        return 0;
    }
    NSString *regExp = @"Usted tiene ([0-9]*) noticias? no leídas?";
    NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSArray *match = [stringData captureComponentsMatchedByRegex:regExp];
    [stringData release];
    if ([match count] < 2)
        return 0;
    return [[match objectAtIndex:1] intValue];
}

The things is that I'm unit testing (using OCUnit) the hole framework but the problem is that I need to simulate/fake what the NSURLConnection is responding in order to test different scenarios and because I can't relay on the server to test my framework.

So the question is Which is the best ways to do this?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about unit-testing