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

  • Home
  • SEARCH
  • 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 904839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:07:02+00:00 2026-05-15T16:07:02+00:00

I Have a Class that needs to make an HTTP request to a server

  • 0

I Have a Class that needs to make an HTTP request to a server in order to get some information. For example:

- (NSUInteger)newsCount {
    NSHTTPURLResponse *response;
    NSError *error;
    NSURLRequest *request = ISKBuildRequestWithURL(ISKDesktopURL, ISKGet, cookie, nil, nil);
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (!data) {
        NSLog(@"The user's(%@) news count could not be obtained:%@", username, [error description]);
        return 0;
    }
    NSString *regExp = @"Usted tiene ([0-9]*) noticias? no leídas?";
    NSString *stringData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSArray *match = [stringData captureComponentsMatchedByRegex:regExp];
    [stringData release];
    if ([match count] < 2)
        return 0;
    return [[match objectAtIndex:1] intValue];
}

The things is that I’m unit testing (using OCUnit) the hole framework but the problem is that I need to simulate/fake what the NSURLConnection is responding in order to test different scenarios and because I can’t relay on the server to test my framework.

So the question is Which is the best ways to do this?

  • 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-05-15T16:07:03+00:00Added an answer on May 15, 2026 at 4:07 pm

    It’s always tricky to test methods that call class methods like NSURLConnection sendSynchronousRequest

    Here are a couple of options:

    a) Use Matt Gallagher’s invokeSupersequent macro to intercept the call. Your unit test would contain code like this:

    @implementation NSURLConneciton (UnitTests)
    
    + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error {
        if (someFlagYourTestUsesToInterceptTheCall) {        
            // return test NSData instance      
        }   
        return invokeSupersequent(request, &response, &error);
    }
    
    @end
    

    Then you set someFlagYourTestUsesToInterceptTheCall to force it to intercept the call and return your test data.

    b) Another alternative is to move that call into its own method in your class under test:

    -(NSData *)retrieveNewsCount:(NSURLRequest *)request {
        NSHTTPURLResponse *response;
        NSError *error;
        return [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    }
    

    Then intercept that call in your test case using OCMock:

    -(void)testNewsCount {
        // instantiate your class
        id myObject = ...;
        id mock = [OCMockObject partialMockForObject:myObject];
        [[[mock stub] andCall:@selector(mockNewsCount:) onObject:self] retrieveNewsCount:[OCMArg any]];
        NSUInteger count = [myObject newsCount];
        // validate response
        ...
    }
    
    // in the same test class:
    -(NSData *)mockNewsCount:(NSURLRequest *)request {
        // return your mock data
        return mockData;
    }
    

    In this case, OCMock’s stub:andCall:onObject:someMethod intercepts just this call to your object’s method in order to inject some test data at test time.

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

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have interpreted the question as being related to project… May 17, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer No. The id used to access widgets from code. If… May 17, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer You just need to install http://docs.moodle.org/en/Multimedia_plugins May 17, 2026 at 12:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using the httprequest class in one of my applications to request a
I'm running this application on a server that has assigned 5 IPs. I use
I have a Requirement to make an IMAP client as a Web application I
I've read through all of the Spring 3 Web docs: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring-web.html but have been
I'm using com.sun.net.httpserver.HttpServer in my project. However, it seems that the server leaks connections
We have a Visual Studio 2010 solution that contains several C# projects in accordance
I'm a beginner in Java programming language, and I have a problem that I
I'm trying to build something (ultimately a gem but for now an application) that
I have an application which will use WCF to serve up various chunks of
Is there a way to get the Content-Type of the response or conversely set

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.