ソースを参照

Adding UNIT tests

Irina Smokvarska 3 年 前
コミット
50efc18787

+ 80 - 0
LiveLikeGiphyChallenge/LiveLikeGiphyChallengeTests/LiveLikeGiphyChallengeTests.swift

@@ -0,0 +1,80 @@
+//
+//  LiveLikeGiphyChallengeTests.swift
+//  LiveLikeGiphyChallengeTests
+//
+//  Created by Irina Smokvarska on 1/30/22.
+//
+
+import XCTest
+@testable import LiveLikeGiphyChallenge
+
+class LiveLikeGiphyChallengeTests: XCTestCase {
+  
+  var urlSession: URLSession!
+  
+  override func setUpWithError() throws {
+    // Set url session for mock networking
+    let configuration = URLSessionConfiguration.ephemeral
+    configuration.protocolClasses = [MockURLProtocol.self]
+    urlSession = URLSession(configuration: configuration)
+  }
+  
+  func testFetchGifsByString_retrieveSearchedGif_successfullyReturnsSearchedGifData() async throws {
+    let searchString = "Adventure%20Time%20Morning"
+    let chunk = 1
+    
+    //The problem with the API here is that it doesn't always return this GIF with the exact same search string
+    let expectedJSON = """
+  [
+      { "type": "gif",
+      "id": "KFOqUTRI8H26s",
+      "url": "https://giphy.com/gifs/KFOqUTRI8H26s",
+      "title": "Adventure Time Morning GIF"
+      }
+  ]
+  """
+ 
+    let mockData = Data(expectedJSON.utf8)
+    MockURLProtocol.requestHandler = { request in
+      (HTTPURLResponse(), mockData)
+    }
+    
+    let service = FetchGiphyAPI()
+    service.urlSession = urlSession
+    let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk)
+    XCTAssertEqual(1, gif.count, "The search result should show 1 GIF")
+  }
+  
+  func testFetchGifsByString_errorWhileRetrievingSearchedGif_failedSearchedGifData() async throws {
+    let searchString = "Adventure%20Time%20Morning"
+    let chunk = -1
+    let expectedJSON = """
+  [
+      { "type": "gif",
+      "id": "KFOqUTRI8H26s",
+      "url": "https://giphy.com/gifs/KFOqUTRI8H26s",
+      "title": "Adventure Time Morning GIF"
+      }
+  ]
+  """
+ 
+    let mockData = Data(expectedJSON.utf8)
+    MockURLProtocol.requestHandler = { request in
+      (HTTPURLResponse(), mockData)
+    }
+    
+    let service = FetchGiphyAPI()
+    service.urlSession = urlSession
+    let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk)
+    XCTAssertEqual([], gif, "The search result should be empty")
+  }
+  
+  func testFetchTrendingGifs_successfullyRetrievesTreningGifs() async throws {
+    let service = FetchGiphyAPI()
+    service.urlSession = urlSession
+   
+    let gifs = try await service.fetchTrendingGifs(chunk: 20)
+    XCTAssertFalse(gifs.isEmpty, "We should get gifs")
+  }
+}
+

+ 40 - 0
LiveLikeGiphyChallenge/LiveLikeGiphyChallengeTests/MockURLProtocol.swift

@@ -0,0 +1,40 @@
+//
+//  MockURLProtocol.swift
+//  LiveLikeGiphyChallenge
+//
+//  Created by Irina Smokvarska on 2/1/22.
+//
+
+import XCTest
+@testable import LiveLikeGiphyChallenge
+
+//MARK: MockURLProtocol
+class MockURLProtocol: URLProtocol {
+  static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))?
+  
+  override class func canInit(with request: URLRequest) -> Bool {
+    return true
+  }
+  
+  override class func canonicalRequest(for request: URLRequest) -> URLRequest {
+    return request
+  }
+  
+  override func startLoading() {
+    guard let handler = MockURLProtocol.requestHandler else {
+      XCTFail("Received unexpected request with no handler set")
+      return
+    }
+    do {
+      let (response, data) = try handler(request)
+      client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
+      client?.urlProtocol(self, didLoad: data)
+      client?.urlProtocolDidFinishLoading(self)
+    } catch {
+      client?.urlProtocol(self, didFailWithError: error)
+    }
+  }
+  
+  override func stopLoading() {
+  }
+}

+ 1 - 0
LiveLikeGiphyChallengeUITests/LiveLikeGiphyChallengeUITests.swift → LiveLikeGiphyChallenge/LiveLikeGiphyChallengeUITests/LiveLikeGiphyChallengeUITests.swift

@@ -2,6 +2,7 @@
 //  LiveLikeGiphyChallengeUITests.swift
 //  LiveLikeGiphyChallengeUITests
 //
+//  Created by Irina Smokvarska on 1/30/22.
 //
 
 import XCTest

+ 1 - 0
LiveLikeGiphyChallengeUITests/LiveLikeGiphyChallengeUITestsLaunchTests.swift → LiveLikeGiphyChallenge/LiveLikeGiphyChallengeUITests/LiveLikeGiphyChallengeUITestsLaunchTests.swift

@@ -2,6 +2,7 @@
 //  LiveLikeGiphyChallengeUITestsLaunchTests.swift
 //  LiveLikeGiphyChallengeUITests
 //
+//  Created by Irina Smokvarska on 1/30/22.
 //
 
 import XCTest

+ 0 - 32
LiveLikeGiphyChallengeTests/LiveLikeGiphyChallengeTests.swift

@@ -1,32 +0,0 @@
-//
-//  LiveLikeGiphyChallengeTests.swift
-//  LiveLikeGiphyChallengeTests
-//
-//
-
-import XCTest
-@testable import LiveLikeGiphyChallenge
-
-class LiveLikeGiphyChallengeTests: XCTestCase {
-
-    override func setUpWithError() throws {
-        // Put setup code here. This method is called before the invocation of each test method in the class.
-    }
-
-    override func tearDownWithError() throws {
-        // Put teardown code here. This method is called after the invocation of each test method in the class.
-    }
-
-    func testExample() throws {
-        // This is an example of a functional test case.
-        // Use XCTAssert and related functions to verify your tests produce the correct results.
-    }
-
-    func testPerformanceExample() throws {
-        // This is an example of a performance test case.
-        self.measure {
-            // Put the code you want to measure the time of here.
-        }
-    }
-
-}