Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:26:20+00:00 2026-06-01T00:26:20+00:00

How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which

  • 0

How can I unit test my NSURLConnection delegate?
I made a ConnectionDelegate class which conforms to different protocols to serve data from the web to different ViewControllers. Before I get too far I want to start writing my unit tests.
But I don’t know how to test them as a unit without the internet connection. I would like also what I should do to treat the asynchronous callbacks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:26:21+00:00Added an answer on June 1, 2026 at 12:26 am

    This is similar to Jon’s response, couldn’t fit it into a comment, though. The first step is to make sure you are not creating a real connection. The easiest way to achieve this is to pull the creation of the connection into a factory method and then substitute the factory method in your test. With OCMock’s partial mock support this could look like this.

    In your real class:

    - (NSURLConnection *)newAsynchronousRequest:(NSURLRequest *)request
    {
        return [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    

    In your test:

    id objectUnderTest = /* create your object */
    id partialMock = [OCMockObject partialMockForObject:objectUnderTest];
    NSURLConnection *dummyUrlConnection = [[NSURLConnection alloc] 
        initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:foo"]] 
        delegate:nil startImmediately:NO];
    [[[partialMock stub] andReturn:dummyUrlConnection] newAsynchronousRequest:[OCMArg any]];
    

    Now, when your object under test tries to create the URL connection it actually gets the dummy connection created in the test. The dummy connection doesn’t have to be valid, because we’re not starting it and it never gets used. If your code does use the connection you could return another mock, one that mocks NSURLConnection.

    The second step is to invoke the method on your object that triggers the creation of the NSURLConnection:

    [objectUnderTest doRequest];
    

    Because the object under test is not using the real connection we can now call the delegate methods from the test. For the NSURLResponse we’re using another mock, the response data is created from a string that’s defined somewhere else in the test:

    int statusCode = 200;
    id responseMock = [OCMockObject mockForClass:[NSHTTPURLResponse class]];
    [[[responseMock stub] andReturnValue:OCMOCK_VALUE(statusCode)] statusCode];
    [objectUnderTest connection:dummyUrlConnection didReceiveResponse:responseMock];
    
    NSData *responseData = [RESPONSE_TEXT dataUsingEncoding:NSASCIIStringEncoding];
    [objectUnderTest connection:dummyUrlConnection didReceiveData:responseData];
    
    [objectUnderTest connectionDidFinishLoading:dummyUrlConnection];
    

    That’s it. You’ve effectively faked all the interactions the object under test has with the connection, and now you can check whether it is in the state it should be in.

    If you want to see some “real” code, have a look at the tests for a class from the CCMenu project that uses NSURLConnections. This is a little bit confusing because the class that’s tested is named connection, too.

    http://ccmenu.svn.sourceforge.net/viewvc/ccmenu/trunk/CCMenuTests/Classes/CCMConnectionTest.m?revision=129&view=markup

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I unit test a method which uses a session object inside of
I am using Moq to mock my Repository layer so I can unit test.
As topic suggested, how can I unit test a function that does not return
If unit-test names can become outdated over time and if you consider that the
Can I use FlexUnit to unit test a File Upload and delete? Are those
How can I write a unit test to test the ActualWidth property in a
Can you share the way you setup your unit test projects within your .net
Can someone tell me why this unit test that checks for exceptions fails? Obviously
I'm writing a unit test and can't remember the syntax for initializing a string
I can typically test a regular Test::Unit method using the following commandline syntax for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.