Stubbing/mocking up webservices for an iOS app

前端 未结 5 575
鱼传尺愫
鱼传尺愫 2021-01-30 08:54

I\'m working on an iOS app whose primary purpose is communication with a set of remote webservices. For integration testing, I\'d like to be able to run my app against some sort

5条回答
  •  [愿得一人]
    2021-01-30 09:36

    OHTTPStubs is a pretty great framework for doing what you want that's gained a lot of traction. From their github readme:

    OHTTPStubs is a library designed to stub your network requests very easily. It can help you:

    • Test your apps with fake network data (stubbed from file) and simulate slow networks, to check your application behavior in bad network conditions
    • Write Unit Tests that use fake network data from your fixtures.

    It works with NSURLConnection, new iOS7/OSX.9's NSURLSession, AFNetworking (both 1.x and 2.x), or any networking framework that use Cocoa's URL Loading System.

    OHHTTPStubs headers are fully documented using Appledoc-like / Headerdoc-like comments in the header files. You can also read the online documentation here.

    Here's an example:

    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return [request.URL.host isEqualToString:@"mywebservice.com"];
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
        // Stub it with our "wsresponse.json" stub file
        NSString* fixture = OHPathForFileInBundle(@"wsresponse.json",nil);
        return [OHHTTPStubsResponse responseWithFileAtPath:fixture
                  statusCode:200 headers:@{@"Content-Type":@"text/json"}];
    }];
    

    You can find additional usage examples on the wiki page.

提交回复
热议问题