block-employee-directory-in.../EmployeeDirectoryTests/EmployeeDirectoryTests.swift
Matt Bruce 8efd109a46 updated test cases for new Testing Framework macros.
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2025-01-21 09:20:41 -06:00

70 lines
2.3 KiB
Swift

//
// EmployeeDirectoryTests.swift
// EmployeeDirectoryTests
//
// Created by Matt Bruce on 1/20/25.
//
import Testing
@testable import EmployeeDirectory
struct EmployeeDirectoryTests {
@Test func getEmployeesValid() async throws {
do {
let wrapper = try await EmployeeService.shared.getEmployees(.production)
#expect(wrapper.employees.count == 11)
} catch {
#expect(Bool(false), "Unexpected error: \(error)")
}
}
@Test func getEmployeesMalformed() async throws {
do {
_ = try await EmployeeService.shared.getEmployees(.malformed)
#expect(Bool(false), "Expected invalidResponse error, but no error was thrown")
} catch let error as NetworkServiceError {
switch error {
case .decodingError(let decodingError):
#expect(Bool(true), "Expected NetworkServiceError.decodingError, but got \(decodingError)")
default:
#expect(Bool(false), "Expected NetworkServiceError.decodingError, but got \(error)")
}
} catch {
#expect(Bool(false), "Unexpected error: \(error)")
}
}
@Test func getEmployeesEmpty() async throws {
do {
let wrapper = try await EmployeeService.shared.getEmployees(.empty)
#expect(wrapper.employees.count == 0)
} catch {
#expect(Bool(false), "Unexpected error: \(error)")
}
}
}
// Testing extension since this isn't needed in the code.
extension NetworkServiceError: @retroactive Equatable {
public static func == (lhs: NetworkServiceError, rhs: NetworkServiceError) -> Bool {
switch (lhs, rhs) {
case (.invalidResponse, .invalidResponse):
return true
case (.invalidURL, .invalidURL):
return true
case (.decodingError(let lhsError), .decodingError(let rhsError)):
return lhsError.localizedDescription == rhsError.localizedDescription
case (.networkError(let lhsError), .networkError(let rhsError)):
return lhsError.code == rhsError.code
case (.unknownError(let lhsError), .unknownError(let rhsError)):
return lhsError.localizedDescription == rhsError.localizedDescription
default:
return false
}
}
}