Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stubs not called for requests sent by RestKit #63

Closed
paristote opened this issue Jun 9, 2014 · 3 comments
Closed

Stubs not called for requests sent by RestKit #63

paristote opened this issue Jun 9, 2014 · 3 comments

Comments

@paristote
Copy link

Hi,

I have a few tests using OHHTTPStubs in my project, and some of them fail because the stub is not called. I know they are not called because a breakpoint on the response block does not pause the execution, and because RestKit complains about the fake URL.

I'm using OHHTTPStubs 3.1.2 installed via CocoaPods, RestKit 0.9.3 (quite old but updating is not possible right now), iOS 7.1 and XCode 5.1.1.

Here's the code of one test that fails:

- (void)testRetrievePlatformInfo
{   
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES;
    } withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"PlatformInfoResponse-4.0.json", nil) statusCode:200 headers:[NSDictionary dictionaryWithObject:@"application/json" forKey:@"Content-Type"]];
    }].name = @"Platform Information";

    [loginProxy retrievePlatformInformations];

    NSLog(@"All stubs: %@", [OHHTTPStubs allStubs]);

    // Wait for the asynchronous code to finish, or a 5s timeout
    NSDate* timeoutDate = [NSDate dateWithTimeIntervalSinceNow:5];
    responseArrived = NO;
    while (!responseArrived && ([timeoutDate timeIntervalSinceNow]>0))
        CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, YES);

    // some assertions
}

The line

[loginProxy retrievePlatformInformations];

calls this method

- (void)retrievePlatformInformations {
    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:[self createBaseURL]];
    [RKObjectManager setSharedManager:manager];

    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[PlatformServerVersion class]];
    [mapping mapKeyPathsToAttributes:
     @"platformVersion",@"platformVersion",
     @"platformRevision",@"platformRevision",
     @"platformBuildNumber",@"platformBuildNumber",
     @"platformEdition",@"platformEdition",
     nil];

    [manager loadObjectsAtResourcePath:@"platform/info" objectMapping:mapping delegate:self];          
}

Displaying the registered stubs shows that it is OK so far:

2014-06-09 17:42:27.959 eXo[30966:60b] All stubs: (
    "<OHHTTPStubsDescriptor 0x9507da0 : Platform Information>"
)

My test class implements delegate methods for handling the response, one for success and one for error. Indeed the error method is called immediately:

2014-06-09 17:42:29.714 eXo[30966:60b] C restkit.network:RKRequest.m:349 SharedClient = <RKClient: 0x9815e90> and network availability = 0
2014-06-09 17:42:50.687 eXo[30966:60b] ERROR: Error Domain=com.twotoasters.RestKit.ErrorDomain Code=2 "The client is unable to contact the resource at http://demo.platform.exo.org/rest/platform/info" UserInfo=0x9816560 {NSLocalizedDescription=The client is unable to contact the resource at http://demo.platform.exo.org/rest/platform/info}

Elsewhere in the code I am logging when stubs are activated

[OHHTTPStubs onStubActivation:^(NSURLRequest *request, id<OHHTTPStubsDescriptor> stub) {
        NSLog(@"%@ request stubbed (%@ %@)", stub.name, request.HTTPMethod, request.URL);
    }];

and surely nothing is logged.

Although RestKit is based on NSURLConnection, when I test another method which sends a request with NSURLConnection directly, this request is stubbed properly...

Finally, I have checked the similar issues #37 #42 and #47 , simplified my Podfile to the maximum, etc but it didn't help.

I am clueless, I might isolate the test case in a separate project for you to see the whole picture, and start upgrading RK if that helps, but meanwhile any idea on this issue would be appreciated.

Cheers,
Philippe

@paristote
Copy link
Author

I've been able to fix my tests by using a real base URL instead of a fake one.
What I think was happening is that RestKit does a reachability test before calling the rest services, and I guess (but not sure) that it happens before stubs are registered.

I'm trying to find more details but it does not seem to be an issue with OHHTTPStubs anyways.

@AliSoftware
Copy link
Owner

Hi @paristote

Thanks for your issue, sorry I didn't have time to help you with it.
Glad you fixed it and thanks for the feedback!

I'll wait for your confirmation then close it afterwards ;)

@paristote
Copy link
Author

I tried to display the network requests with tcpdump, but nothing appeared. Either my filter was incorrect, or there's something else. I could try with my own server and reading the logs but I don't have so much time so I'll leave it for later.

You can close this issue, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants