2
\$\begingroup\$

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! =)

asked Jan 5, 2013 at 22:42
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 7, 2013 at 14:35
  • \$\begingroup\$ but this code is working. I just want to improve it to provide better error explanation. This code isn't faulty. \$\endgroup\$ Commented Jan 7, 2013 at 14:51
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 7, 2013 at 14:52
  • \$\begingroup\$ @vikingosegundo do you have any thoughts regarding the question itself? \$\endgroup\$ Commented Jan 7, 2013 at 15:00
  • \$\begingroup\$ Nope, I never worked with Kiwi. But sure I will try it out soon. +1 \$\endgroup\$ Commented Jan 7, 2013 at 15:02

1 Answer 1

1
\$\begingroup\$

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?

answered Jan 7, 2013 at 14:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.