Skip to content

Commit 16daea2

Browse files
authored
Fix tests and make sure the new opt-in mode is working with existing logic. (#86)
1 parent a19b459 commit 16daea2

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

MockerTests/MockerTests.swift

+30-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ final class MockerTests: XCTestCase {
2020
}
2121
}
2222

23+
override func setUp() {
24+
super.setUp()
25+
Mocker.mode = .optout
26+
}
27+
28+
override func tearDown() {
29+
Mocker.removeAll()
30+
Mocker.mode = .optout
31+
super.tearDown()
32+
}
33+
2334
/// It should returned the register mocked image data as response.
2435
func testImageURLDataRequest() {
2536
let expectation = self.expectation(description: "Data request should succeed")
@@ -77,7 +88,10 @@ final class MockerTests: XCTestCase {
7788

7889
URLSession.shared.dataTask(with: originalURL) { (data, _, error) in
7990
XCTAssert(error == nil)
80-
let image: UIImage = UIImage(data: data!)!
91+
guard let data = data, let image: UIImage = UIImage(data: data) else {
92+
XCTFail("Invalid data")
93+
return
94+
}
8195
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!
8296

8397
XCTAssert(image.size == sampleImage.size, "Image should be returned mocked")
@@ -101,7 +115,10 @@ final class MockerTests: XCTestCase {
101115

102116
URLSession.shared.dataTask(with: customURL) { (data, _, error) in
103117
XCTAssert(error == nil)
104-
let image: UIImage = UIImage(data: data!)!
118+
guard let data = data, let image: UIImage = UIImage(data: data) else {
119+
XCTFail("Invalid data")
120+
return
121+
}
105122
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!
106123

107124
XCTAssert(image.size == sampleImage.size, "Image should be returned mocked")
@@ -188,7 +205,10 @@ final class MockerTests: XCTestCase {
188205

189206
urlSession.dataTask(with: originalURL!) { (data, _, error) in
190207
XCTAssert(error == nil)
191-
let image: UIImage = UIImage(data: data!)!
208+
guard let data = data, let image: UIImage = UIImage(data: data) else {
209+
XCTFail("Invalid data")
210+
return
211+
}
192212
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!
193213

194214
XCTAssert(image.size == sampleImage.size, "Image should be returned mocked")
@@ -389,22 +409,24 @@ final class MockerTests: XCTestCase {
389409
let expectation = self.expectation(description: "Data request should succeed")
390410
let originalURL = URL(string: "https://www.wetransfer.com/example.json")!
391411

392-
enum TestExampleError: Error {
412+
enum TestExampleError: Error, LocalizedError {
393413
case example
414+
415+
var errorDescription: String { "example" }
394416
}
395417

396418
Mock(url: originalURL, dataType: .json, statusCode: 500, data: [.get: Data()], requestError: TestExampleError.example).register()
397419

398-
URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, err) in
420+
URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, error) in
399421

400422
XCTAssertNil(data)
401423
XCTAssertNil(urlresponse)
402-
XCTAssertNotNil(err)
403-
if let err = err {
424+
XCTAssertNotNil(error)
425+
if let error = error {
404426
// there's not a particularly elegant way to verify an instance
405427
// of an error, but this is a convenient workaround for testing
406428
// purposes
407-
XCTAssertEqual("example", String(describing: err))
429+
XCTAssertTrue(String(describing: error).contains("TestExampleError"))
408430
}
409431

410432
expectation.fulfill()

Sources/Mocker.swift

+9-7
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,23 @@ public struct Mocker {
102102
///
103103
/// - Parameter url: The URL to check for.
104104
/// - Returns: `true` if it should be mocked, `false` if the URL is registered as ignored.
105-
public static func shouldHandle(_ url: URL) -> Bool {
106-
shared.queue.sync {
107-
switch mode {
108-
case .optout:
109-
return !shared.ignoredRules.contains(where: { $0.shouldIgnore(url) })
110-
case .optin:
111-
return shared.mocks.contains(where: { $0.url == url })
105+
public static func shouldHandle(_ request: URLRequest) -> Bool {
106+
switch mode {
107+
case .optout:
108+
guard let url = request.url else { return false }
109+
return shared.queue.sync {
110+
!shared.ignoredRules.contains(where: { $0.shouldIgnore(url) })
112111
}
112+
case .optin:
113+
return mock(for: request) != nil
113114
}
114115
}
115116

116117
/// Removes all registered mocks. Use this method in your tearDown function to make sure a Mock is not used in any other test.
117118
public static func removeAll() {
118119
shared.queue.sync(flags: .barrier) {
119120
shared.mocks.removeAll()
121+
shared.ignoredRules.removeAll()
120122
}
121123
}
122124

Sources/MockingURLProtocol.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public final class MockingURLProtocol: URLProtocol {
8888

8989
/// Overrides needed to define a valid inheritance of URLProtocol.
9090
override public class func canInit(with request: URLRequest) -> Bool {
91-
guard let url = request.url else { return false }
92-
return Mocker.shouldHandle(url)
91+
return Mocker.shouldHandle(request)
9392
}
9493
}
9594

0 commit comments

Comments
 (0)