Skip to content

Commit

Permalink
Fix tests and make sure the new opt-in mode is working with existing …
Browse files Browse the repository at this point in the history
…logic. (#86)
  • Loading branch information
AvdLee authored Feb 26, 2021
1 parent a19b459 commit 16daea2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
38 changes: 30 additions & 8 deletions MockerTests/MockerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ final class MockerTests: XCTestCase {
}
}

override func setUp() {
super.setUp()
Mocker.mode = .optout
}

override func tearDown() {
Mocker.removeAll()
Mocker.mode = .optout
super.tearDown()
}

/// It should returned the register mocked image data as response.
func testImageURLDataRequest() {
let expectation = self.expectation(description: "Data request should succeed")
Expand Down Expand Up @@ -77,7 +88,10 @@ final class MockerTests: XCTestCase {

URLSession.shared.dataTask(with: originalURL) { (data, _, error) in
XCTAssert(error == nil)
let image: UIImage = UIImage(data: data!)!
guard let data = data, let image: UIImage = UIImage(data: data) else {
XCTFail("Invalid data")
return
}
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!

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

URLSession.shared.dataTask(with: customURL) { (data, _, error) in
XCTAssert(error == nil)
let image: UIImage = UIImage(data: data!)!
guard let data = data, let image: UIImage = UIImage(data: data) else {
XCTFail("Invalid data")
return
}
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!

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

urlSession.dataTask(with: originalURL!) { (data, _, error) in
XCTAssert(error == nil)
let image: UIImage = UIImage(data: data!)!
guard let data = data, let image: UIImage = UIImage(data: data) else {
XCTFail("Invalid data")
return
}
let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)!

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

enum TestExampleError: Error {
enum TestExampleError: Error, LocalizedError {
case example

var errorDescription: String { "example" }
}

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

URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, err) in
URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, error) in

XCTAssertNil(data)
XCTAssertNil(urlresponse)
XCTAssertNotNil(err)
if let err = err {
XCTAssertNotNil(error)
if let error = error {
// there's not a particularly elegant way to verify an instance
// of an error, but this is a convenient workaround for testing
// purposes
XCTAssertEqual("example", String(describing: err))
XCTAssertTrue(String(describing: error).contains("TestExampleError"))
}

expectation.fulfill()
Expand Down
16 changes: 9 additions & 7 deletions Sources/Mocker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,23 @@ public struct Mocker {
///
/// - Parameter url: The URL to check for.
/// - Returns: `true` if it should be mocked, `false` if the URL is registered as ignored.
public static func shouldHandle(_ url: URL) -> Bool {
shared.queue.sync {
switch mode {
case .optout:
return !shared.ignoredRules.contains(where: { $0.shouldIgnore(url) })
case .optin:
return shared.mocks.contains(where: { $0.url == url })
public static func shouldHandle(_ request: URLRequest) -> Bool {
switch mode {
case .optout:
guard let url = request.url else { return false }
return shared.queue.sync {
!shared.ignoredRules.contains(where: { $0.shouldIgnore(url) })
}
case .optin:
return mock(for: request) != nil
}
}

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

Expand Down
3 changes: 1 addition & 2 deletions Sources/MockingURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public final class MockingURLProtocol: URLProtocol {

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

Expand Down

0 comments on commit 16daea2

Please sign in to comment.