Database Reference
In-Depth Information
a single class. This has the advantage of letting you test your code in a clean-room, as
it were, but requires you to create a lot more test framework. It's not my place to
prescribe a unit testing approach, but I'll say that I find that making some compromises
here can let you test effectively without having to recreate the entire outside world in
mocks.
We have a few classes it would be useful to unit test from the Buggy Whip Chat ap-
plication (you first saw these back in Chapter 4 ), namely the URL endpoint generators.
So, to begin our testing effort, let's tackle them.
We start, as described above, by adding a new unit test class to our unit test target.
Since we're testing the WebServiceEndpointGenerator class, we'll call it TestWeb
ServiceEndpointGenerator . For some reason (as of this writing, as with everything in
the topic), creating a unit test project gives you a nice clean sample test case file, but
adding a new test class gives you the old template, full of conditionals for in-app vs.
static testing that don't really apply anymore since unit tests now always run on the
simulator, not at compile time. So, the first thing we can do is to strip out all the
conditionalization logic in the header and code files, leaving them ending much more
like the sample file you get with the target. When we're done, they should look like the
code in Example 5-1 .
Example 5-1. The header and code files for a bare test case
//// TestWebServiceEndpointGenerator.h
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
@interface TestWebServiceEndpointGenerator : SenTestCase
@end
//// TestWebServiceEndpointGenerator.m
#import "TestWebServiceEndpointGenerator.h"
@implementation TestWebServiceEndpointGenerator
@end
The first method to test is the getForecastWebServiceEndpoint method. So that we can
do negative as well as positive testing, the method has been rewritten slightly to add
some argument checking at the top:
+(NSURL *) getForecastWebServiceEndpoint:
(NSString *) zipcode
startDate:(NSDate *) startDate
endDate:(NSDate *) endDate {
if ((zipcode == NULL) || (startDate == NULL) ||
(endDate == NULL)) {
 
Search WWH ::




Custom Search