Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 007e11a

Browse files
committed
Use alternative approach to XCTUnwrap for stubRemoteResponse
1 parent 9a59c67 commit 007e11a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

WordPressKitTests/RemoteTestCase.swift

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ extension RemoteTestCase {
5454
contentType: ResponseContentType,
5555
status: Int32 = 200
5656
) {
57-
// This doesn't follow the XCTUnwrap pattern (yet?) because the method is used many times and it was too time consuming to update every call site at the time
58-
let stubPath = OHPathForFile(filename, type(of: self))
57+
// There are hundreds of usages of the various `stubRemoteResponse` overloads.
58+
// The pattern here should be to XCTUnwrap and throw.
59+
// In the interest of moving along with the work, let's fail the tests at this level if the file is not found.
60+
guard let stubPath = OHPathForFile(filename, type(of: self)) else {
61+
return XCTFail("Could not find file at path '\(filename)'.")
62+
}
63+
5964
stub(condition: condition) { _ in
6065
var headers: [NSObject: AnyObject]?
6166

6267
if contentType != .NoContentType {
6368
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
6469
}
65-
// This is force-unwrapped at call site, despite it making more sense at declaration site, so it can be found when grepping for force unwraps.
66-
return OHHTTPStubs.fixture(filePath: stubPath!, status: status, headers: headers)
70+
return OHHTTPStubs.fixture(filePath: stubPath, status: status, headers: headers)
6771
}
6872
}
6973

@@ -76,8 +80,13 @@ extension RemoteTestCase {
7680
/// - status: The status code to use for the response. Defaults to 200.
7781
///
7882
func stubRemoteResponse(_ endpoint: String, filename: String, contentType: ResponseContentType, status: Int32 = 200) {
79-
// This doesn't follow the XCTUnwrap pattern (yet?) because the method is used many times and it was too time consuming to update every call site at the time
80-
let stubPath = OHPathForFile(filename, type(of: self))
83+
// There are hundreds of usages of the various `stubRemoteResponse` overloads.
84+
// The pattern here should be to XCTUnwrap and throw.
85+
// In the interest of moving along with the work, let's fail the tests at this level if the file is not found.
86+
guard let stubPath = OHPathForFile(filename, type(of: self)) else {
87+
return XCTFail("Could not find file at path '\(filename)'.")
88+
}
89+
8190
stub(condition: { request in
8291
return request.url?.absoluteString.range(of: endpoint) != nil
8392
}) { _ in
@@ -86,8 +95,7 @@ extension RemoteTestCase {
8695
if contentType != .NoContentType {
8796
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
8897
}
89-
// This is force-unwrapped at call site, despite it making more sense at declaration site, so it can be found when grepping for force unwraps.
90-
return fixture(filePath: stubPath!, status: status, headers: headers)
98+
return fixture(filePath: stubPath, status: status, headers: headers)
9199
}
92100
}
93101

@@ -138,15 +146,20 @@ extension RemoteTestCase {
138146
return HTTPStubsResponse(error: notConnectedError)
139147
}
140148

141-
let stubPath = OHPathForFile(files[callCounter], type(of: self))
149+
guard let stubPath = OHPathForFile(files[callCounter], type(of: self)) else {
150+
XCTFail("Could not find file at path '\(files[callCounter])'.")
151+
let error = NSError(domain: "RemoteTestCase", code: 0, userInfo: nil)
152+
return HTTPStubsResponse(error: error)
153+
}
154+
142155
callCounter += 1
143156

144157
var headers: [NSObject: AnyObject]?
145158
if contentType != .NoContentType {
146159
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
147160
}
148161

149-
return fixture(filePath: stubPath!, status: status, headers: headers)
162+
return fixture(filePath: stubPath, status: status, headers: headers)
150163
}
151164
}
152165

0 commit comments

Comments
 (0)