0

I want to test a method which makes URL request. When response is received in NSURLConnectionDelegate method I save the response. The logic I really want to test is whether response was saved or not.

How to test NSURLConnectionDelegate method?

Method I want to test :

- (void) getProfilePictureURL :(NSString *)fbId
{
    //to get picture URL
    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", fbId];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (!connection)
    {
        NSLog(@"error while starting the connection");
    }
} 

// NSURLConnectionDelegate method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Save response in user defaults
}
Geek
  • 8,280
  • 17
  • 73
  • 137
  • check if the response is saved in userdefault or not?? thats where you can identify that data is saved. – Armaan Stranger Aug 22 '13 at 08:47
  • I know that but I can test it only after connection delegate is called and code is executed. How do I check that delegate is called? – Geek Aug 22 '13 at 08:49
  • sorry but i can't understand your scenario. you want to check it before saving response or after saving response ?? – Armaan Stranger Aug 22 '13 at 08:52
  • Obviously after saving response. But I have to wait until connection delegate is called. Then only I can test. – Geek Aug 22 '13 at 09:02
  • obviously. delegate methods saves the data in userdefault then you need to check it there. otherwise there will be no data before that delegate method is called. – Armaan Stranger Aug 22 '13 at 09:11
  • In unit testing how do I check when delegate method is called? – Geek Aug 22 '13 at 09:19
  • have you stored data using any key in userdefaults? – Armaan Stranger Aug 22 '13 at 09:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35983/discussion-between-akash-and-armaan-stranger) – Geek Aug 22 '13 at 09:32

1 Answers1

0

The NSURLConnection class does not have any methods of its own to return the http response code so treats a 404 as a successful response - which I suppose it is as a connection was made and something (the 404 page) was returned.

The trick to see if the response was a 200 OK or 404 NOT FOUND is to cast the NSURLResponse to a NSHTTPURLResponse and then use that classes methods to look for the http response code.

Note: If the connection was made the delegate will be called definitely. You should just check if the connection is not made, then what you should be doing.

anky_believeMe
  • 484
  • 1
  • 4
  • 14
  • I have some code in delegate method and I want to test that code. So I can test only once delegate is called and code is executed. – Geek Aug 22 '13 at 10:37
  • So that is the question that how to do it? means HOW TO WAIT for delegate to be called? – Geek Aug 22 '13 at 10:44
  • You dont have to wait for it to be called. As soon as you request for a connection it will automatically go in those delegate functions even if there is no internet connection. – anky_believeMe Aug 22 '13 at 10:50
  • Also make sure you have added `NSURLConnectionDelegate` in your header file. – anky_believeMe Aug 22 '13 at 10:53
  • It takes a second to call delegate and before that my test method gets executed. – Geek Aug 22 '13 at 10:59
  • Is your delegate method getting called.? Did you check.? – anky_believeMe Aug 22 '13 at 11:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35994/discussion-between-akash-and-believe-anky) – Geek Aug 22 '13 at 11:15