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! =)
-
\$\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\$vikingosegundo– vikingosegundo2013年01月07日 14:35:13 +00:00Commented 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\$yas375– yas3752013年01月07日 14:51:28 +00:00Commented 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\$vikingosegundo– vikingosegundo2013年01月07日 14:52:54 +00:00Commented Jan 7, 2013 at 14:52
-
\$\begingroup\$ @vikingosegundo do you have any thoughts regarding the question itself? \$\endgroup\$yas375– yas3752013年01月07日 15:00:09 +00:00Commented Jan 7, 2013 at 15:00
-
\$\begingroup\$ Nope, I never worked with Kiwi. But sure I will try it out soon. +1 \$\endgroup\$vikingosegundo– vikingosegundo2013年01月07日 15:02:26 +00:00Commented Jan 7, 2013 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?