Skip to content

Commit

Permalink
Merge pull request #787 from DeluxeAlonso/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
DeluxeAlonso committed Sep 13, 2023
2 parents defa6a0 + 11e78a4 commit 73aec98
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2019 Alonso. All rights reserved.
//

struct RequestTokenResult: Decodable {
struct RequestTokenResult: Codable {

let success: Bool
let token: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// AuthClientTests.swift
// NetworkInfrastructure-Unit-NetworkInfrastructureTests
//
// Created by Alonso on 12/09/23.
//

import XCTest
@testable import NetworkInfrastructure

final class AuthClientTests: XCTestCase {

private var urlSession: MockURLSession!
private var authClient: AuthClient!

override func setUpWithError() throws {
try super.setUpWithError()
urlSession = MockURLSession()
authClient = AuthClient(session: urlSession)
}

override func tearDownWithError() throws {
urlSession = nil
authClient = nil
try super.tearDownWithError()
}

func testGetRequestTokenSuccess() throws {
// Arrange
let data = try JSONEncoder().encode(RequestTokenResult(success: true, token: ""))
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 request token success")
// Act
authClient.getRequestToken(with: "") { result in
switch result {
case .success:
break
case .failure:
XCTFail("Get request token error")
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

func testGetRequestTokenError() throws {
// Arrange
urlSession.dataTaskWithRequestCompletionHandler = (nil, nil, nil)
let expectation = XCTestExpectation(description: "Get request token error")
// Act
authClient.getRequestToken(with: "") { result in
switch result {
case .success:
XCTFail("Get request token success")
case .failure:
break
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

}

0 comments on commit 73aec98

Please sign in to comment.