适用于 iOS 和 macOS 的 HTTP/HTTPS 请求存根。它与 NSURLSession、AFNetworking、Alamofire 或任何使用 Cocoa URL 加载系统的网络框架一起工作。
WebMock 使用会话配置的替换而不是默认配置。
let defaultConfiguration = NSURLSessionConfiguration.webmockDefaultSessionConfiguration()
let ephemeralConfiguration = NSURLSessionConfiguration.webmockEphemeralSessionConfiguration()
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)
let response = Response(data: data)
let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)
WebMock.startWithStubs([stub])
let json = ["testKey": "testValue"]
let response = Response(json: json)
let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)
WebMock.startWithStubs([stub])
let fileURL = bundle.pathForResource("test", ofType: "json")!
let response = Response(fileURL: fileURL)
let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)
WebMock.startWithStubs([stub])
let error = NSError(domain: NSInternalInconsistencyException,
code: 0,
userInfo: nil)
let response = Response(error: error)
let URL = NSURL(string: "https://www.google.com")!
let stub = Stub(method: .GET, URL: URL, response: response)
WebMock.startWithStubs([stub])
您可以额外定义响应的头部字段和 HTTP 方法。
手动将文件添加到 Xcode 项目中。稍微简单一点,但更新也是手动的。
Wojtek Lukaszuk @wojteklu
WebMock 依据 MIT 许可协议提供。查阅 LICENSE 文件获取更多信息。