You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It'd be really nice if there was a fluent API for stubbing. I personally feel this drastically helps readability and has the added benefit of making it very simple to return different responses for the same condition.
Ideal API (in my head):
func testExample()asyncthrows{letexpectedData=tryXCTUnwrap(UUID().uuidString.data(using:.utf8))letsecondData=tryXCTUnwrap(UUID().uuidString.data(using:.utf8))StubResponse(on:isHost("www.someapi.com") && isMethodGET()){ req inHTTPStubsResponse(data: expectedData, statusCode:500, headers:nil)}.thenRespond(on:isHost("www.someapi.com") && isMethodGET()){ req inHTTPStubsResponse(data: secondData, statusCode:400, headers:nil)}let(data, response)=tryawaitURLSession.shared.data(from:URL(string:"https://www.someapi.com")!)XCTAssertEqual((response as?HTTPURLResponse)?.statusCode,500)// first respond with a 500XCTAssertEqual(data, expectedData)let(data2, response2)=tryawaitURLSession.shared.data(from:URL(string:"https://www.someapi.com")!)XCTAssertEqual((response2 as?HTTPURLResponse)?.statusCode,400)// then respond with a 400XCTAssertEqual(data2, secondData)let(data3, response3)=tryawaitURLSession.shared.data(from:URL(string:"https://www.someapi.com")!)XCTAssertEqual((response3 as?HTTPURLResponse)?.statusCode,400)// 400 response stays stubbed, cause that was the last oneXCTAssertEqual(data3, secondData)}
WIP - (The thing I usually rebuild)
I thought I'd share code I often rewrite when I pull in this library so that others might benefit. It has some drawbacks, it's not particularly efficient, and it's a little questionable how it removes stubs, but I think it's a useful copy/paste if you also like this sort of thing.
import OHHTTPStubs
import OHHTTPStubsSwift
finalclassStubResponse{varstubs:[(condition:HTTPStubsTestBlock, response:HTTPStubsResponseBlock)]=[]@discardableResultconvenienceinit(on condition:@escapingHTTPStubsTestBlock, with response:@escapingHTTPStubsResponseBlock){self.init(stubs:[(condition, response)])}privateinit(stubs:[(condition:HTTPStubsTestBlock, response:HTTPStubsResponseBlock)]){self.stubs = stubs
stub{ req inletfiltered=self.stubs.enumerated().filter{ $0.element.condition(req)}return !filtered.isEmpty
} response:{ req inletfiltered=self.stubs.enumerated().filter{ $0.element.condition(req)}
guard let first = filtered.first else{fatalError("No stub response found, something bad happened")}defer{
if filtered.count >1,let first = filtered.first {self.stubs.remove(at: first.offset)}}return first.element.response(req)}}@discardableResultfunc thenRespond(on condition:@escapingHTTPStubsTestBlock, with response:@escapingHTTPStubsResponseBlock)->Self{Self(stubs: stubs.appending((condition, response)))}}extensionArray{fileprivatefunc appending(_ element:Element)->Self{varcopy=self
copy.append(element)return copy
}}
I would submit a PR but I'm not sure if others would find this useful and didn't want to spend time resolving the issues mentioned unless this is useful to anybody other than me.
The text was updated successfully, but these errors were encountered:
It'd be really nice if there was a fluent API for stubbing. I personally feel this drastically helps readability and has the added benefit of making it very simple to return different responses for the same condition.
Ideal API (in my head):
WIP - (The thing I usually rebuild)
I thought I'd share code I often rewrite when I pull in this library so that others might benefit. It has some drawbacks, it's not particularly efficient, and it's a little questionable how it removes stubs, but I think it's a useful copy/paste if you also like this sort of thing.
I would submit a PR but I'm not sure if others would find this useful and didn't want to spend time resolving the issues mentioned unless this is useful to anybody other than me.
The text was updated successfully, but these errors were encountered: