Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I would like to get an advice regarding how to test the same code with different input data.

I would like to test that method operationSucceeded will be invoked for all successful status codes. Here is how I can test (with Kiwi) single status code (i.e. 200):

it(@"should call operationSucceeded", ^{
    MKNetworkOperation *op =[[MKNetworkOperation alloc] initWithURLString:@"http://example.com/api" params:nil httpMethod:@"GET"];
    [op stub:@selector(notifyCache)];
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:nil
                                                              statusCode:200
                                                             HTTPVersion:nil
                                                            headerFields:nil];
    [op stub:@selector(response) andReturn:response];
    [[op should] receive:@selector(operationSucceeded)];
    [op connectionDidFinishLoading:[NSURLConnection nullMock]];
});

For now I do next:

it(@"should call operationSucceeded", ^{
    NSArray *successCodes = @[@200, @201, @202, @203, @204, @205, @206];
    for (NSNumber *statusCode in successCodes) {
        MKNetworkOperation *op =[[MKNetworkOperation alloc] initWithURLString:@"http://example.com/api" params:nil httpMethod:@"GET"];
        [op stub:@selector(notifyCache)];
        NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:nil
                                                                  statusCode:[statusCode intValue]
                                                                 HTTPVersion:nil
                                                                headerFields:nil];
        [op stub:@selector(response) andReturn:response];
        [[op should] receive:@selector(operationSucceeded)];
        [op connectionDidFinishLoading:[NSURLConnection nullMock]];
    }
});

But the main problem here is that if my test fail for one of status codes it will say something like: "'Operation, should call operationSucceeded' [FAILED], expected subject to receive -operationSucceeded exactly 1 time, but received it 0 times" which is not very useful.

You can find full source code here.

Any advices are very welcome! =)

share|improve this question
    
This site is dedicated for code review: you can present working code and ask for possible improvements. However if the code is faulty it isn't in the scope of this site and you should post it on stackoverflow. Also the audience over there is much bigger. –  vikingosegundo Jan 7 '13 at 14:35
    
but this code is working. I just want to improve it to provide better error explanation. This code isn't faulty. –  yas375 Jan 7 '13 at 14:51
    
than you should work on the question's text, as I understood as if it isnt doing what you expect. probably others will misread it as-well. –  vikingosegundo Jan 7 '13 at 14:52
    
@vikingosegundo do you have any thoughts regarding the question itself? –  yas375 Jan 7 '13 at 15:00
    
Nope, I never worked with Kiwi. But sure I will try it out soon. +1 –  vikingosegundo Jan 7 '13 at 15:02

1 Answer 1

I've got one possible solution from Max Lunin:

NSArray *successCodes = @[@200, @201, @202, @203, @204, @205, @206];
for (NSNumber *statusCode in successCodes) {
    it([NSString stringWithFormat:@"should call operationSucceeded for status code %@", statusCode], ^{
        MKNetworkOperation *op =[[MKNetworkOperation alloc] initWithURLString:@"http://example.com/api" params:nil httpMethod:@"GET"];
        [op stub:@selector(notifyCache)];
        NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:nil
                                                                  statusCode:[statusCode intValue]
                                                                 HTTPVersion:nil
                                                                headerFields:nil];
        [op stub:@selector(response) andReturn:response];
        [[op should] receive:@selector(operationSucceeded)];
        [op connectionDidFinishLoading:[NSURLConnection nullMock]];
    });
}

It solves the problem with error explanation. It will tell something like: 'Operation, should call operationSucceeded for status code 202' [FAILED], expected subject to receive -operationSucceeded exactly 1 time, but received it 0 times

Any other more clear solutions?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.