Stubbing/mocking up webservices for an iOS app

前端 未结 5 577
鱼传尺愫
鱼传尺愫 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:51

    I'd suggest to use Nocilla. Nocilla is a library for stubbing HTTP requests with a simple DSL.

    Let's say that you want to return a 404 from google.com. All you have to do is:

    stubRequest(@"GET", "http://www.google.com").andReturn(404); // Yes, it's ObjC
    

    After that, any HTTP to google.com will return a 404.

    A more complete example, where you want to match a POST with a certain body and headers and return a canned response:

    stubRequest(@"POST", @"https://api.example.com/dogs.json").
    withHeaders(@{@"Accept": @"application/json", @"X-CUSTOM-HEADER": @"abcf2fbc6abgf"}).
    withBody(@"{\"name\":\"foo\"}").
    andReturn(201).
    withHeaders(@{@"Content-Type": @"application/json"}).
    withBody(@"{\"ok\":true}");
    

    You can match any request and fake any response. Check the README for more details.

    The benefits of using Nocilla over other solutions are:

    • It's fast. No HTTP servers to run. Your tests will run really fast.
    • No crazy dependencies to manage. On top of that, you can use CocoaPods.
    • It's well tested.
    • Great DSL that will make your code really easy to understand and maintain.

    The main limitation is that it only works with HTTP frameworks built on top of NSURLConnection, like AFNetworking, MKNetworkKit or plain NSURLConnection.

    Hope this helps. If you need anything else, I'm here to help.

提交回复
热议问题