I am working on a custom networking library and writing some unit test cases for it. I am not sure how to go about it.
I have a RequestObject which is a subclass of NSOperation
@interface RequestOperation : NSOperation @property (nonatomic, strong) NSURLRequest *URLRequest; @property (nonatomic, strong, readonly) NSURLResponse *URLResponse; @property (nonatomic, strong, readonly) NSError *connectionError; @property (nonatomic, strong, readonly) NSData *responseData; -(id)initWithURLRequest:(NSURLRequest*)request; @end
In implementation I have a private category which has NSURLConnection.
I want to write a test case to check if URLResponse exist after the request is being sent
-(void)testIfResponseExist { NSURL *url = [[NSURL alloc] initWithString:@"https://google.com"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; myRequest = [[RequestOperation alloc] initWithURLRequest:request]; [myRequest start]; [self WaitFor:1]; XCTAssertNotNil(myRequest.URLResponse, @"Response should not be nil"); }
Is this way of unit testing correct ?
Now this is sending the actual request to the server. However, I can also stub NSURLConnection and send dummy response. But I am not sure which way I should go. What are pros and cons of mock object ?