Skip to content

Commit

Permalink
Merge pull request #765 from DeluxeAlonso/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
DeluxeAlonso authored Aug 24, 2023
2 parents 238c6d4 + 0bc060c commit 7ad430e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UpcomingMoviesDomain

struct Movie: Decodable {
struct Movie: Codable {

let id: Int
let title: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2018 Alonso. All rights reserved.
//

struct MovieResult: Decodable, Paginable {
struct MovieResult: Codable, Paginable {

let results: [Movie]
var currentPage: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ final class MovieClient: APIClient, MovieClientProtocol {

// MARK: - Initializers

init(configuration: URLSessionConfiguration) {
self.session = URLSession(configuration: configuration)
init(session: URLSession) {
self.session = session
}

convenience init() {
let configuration: URLSessionConfiguration = .default
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData

self.init(configuration: configuration)
self.init(session: URLSession(configuration: configuration))
}

// MARK: - Movie list
Expand Down
59 changes: 47 additions & 12 deletions NetworkInfrastructure/Services/MovieClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,65 @@
//

import XCTest
@testable import NetworkInfrastructure

final class MovieClientTests: XCTestCase {

private var urlSession: MockURLSession!
private var movieClient: MovieClient!

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
try super.setUpWithError()
urlSession = MockURLSession()
movieClient = MovieClient(session: urlSession)

}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
urlSession = nil
movieClient = nil
try super.tearDownWithError()
}

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.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
func testGetPopularMoviesSuccess() throws {
// Arrange
let data = try JSONEncoder().encode(MovieResult(results: [], currentPage: 1, totalPages: 1))
guard let url = URL(string: "www.google.com") else {
XCTFail("Invalid URL")
return
}
urlSession.dataTaskWithRequestCompletionHandler = (data, HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil), nil)
let expectation = XCTestExpectation(description: "Get popular movies success")
// Act
movieClient.getPopularMovies(page: 1) { result in
switch result {
case .success:
break
case .failure:
XCTFail("Get popular movies error")
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

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.
func testGetPopularMoviesError() throws {
// Arrange
urlSession.dataTaskWithRequestCompletionHandler = (nil, nil, nil)
let expectation = XCTestExpectation(description: "Get popular movies error")
// Act
movieClient.getPopularMovies(page: 1) { result in
switch result {
case .success:
XCTFail("Get popular movies success")
case .failure:
break
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

}

0 comments on commit 7ad430e

Please sign in to comment.