Skip to content

Commit

Permalink
REFACTOR: Migrate Company tests to Swift Testing (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Sep 17, 2024
1 parent 6900031 commit 44acc55
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,42 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class CompanyDetailsRequestTests: XCTestCase {
@Suite(.tags(.requests, .company))
struct CompanyDetailsRequestTests {

var request: CompanyDetailsRequest!

override func setUp() {
super.setUp()
request = CompanyDetailsRequest(id: 1)
init() {
self.request = CompanyDetailsRequest(id: 1)
}

override func tearDown() {
request = nil
super.tearDown()
@Test("path is correct")
func path() {
#expect(request.path == "/company/1")
}

func testPath() {
XCTAssertEqual(request.path, "/company/1")
@Test("queryItems is empty")
func queryItemsAreEmpty() {
#expect(request.queryItems.isEmpty)
}

func testQueryItemsAreEmpty() {
XCTAssertTrue(request.queryItems.isEmpty)
@Test("method is GET")
func methodIsGet() {
#expect(request.method == .get)
}

func testMethodIsGet() {
XCTAssertEqual(request.method, .get)
@Test("headers is empty")
func headersIsEmpty() {
#expect(request.headers.isEmpty)
}

func testHeadersIsEmpty() {
XCTAssertTrue(request.headers.isEmpty)
}

func testBodyIsNil() {
XCTAssertNil(request.body)
@Test("body is nil")
func bodyIsNil() {
#expect(request.body == nil)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class TMDbCompanyServiceTests: XCTestCase {
@Suite(.tags(.services, .company))
struct TMDbCompanyServiceTests {

var service: TMDbCompanyService!
var apiClient: MockAPIClient!

override func setUp() {
super.setUp()
apiClient = MockAPIClient()
service = TMDbCompanyService(apiClient: apiClient)
init() {
self.apiClient = MockAPIClient()
self.service = TMDbCompanyService(apiClient: apiClient)
}

override func tearDown() {
apiClient = nil
service = nil
super.tearDown()
}

func testDetailsReturnsCompany() async throws {
@Test("details returns company")
func detailsReturnsCompany() async throws {
let expectedResult = Company.lucasfilm
let companyID = expectedResult.id
let expectedRequest = CompanyDetailsRequest(id: companyID)
Expand All @@ -46,25 +42,18 @@ final class TMDbCompanyServiceTests: XCTestCase {

let result = try await service.details(forCompany: companyID)

XCTAssertEqual(result, expectedResult)
XCTAssertEqual(apiClient.lastRequest as? CompanyDetailsRequest, expectedRequest)
#expect(result == expectedResult)
#expect(apiClient.lastRequest as? CompanyDetailsRequest == expectedRequest)
}

func testDetailsWhenErrorsThrowsError() async throws {
@Test("details when errors throws error")
func detailsWhenErrorsThrowsError() async throws {
let companyID = 1

apiClient.addResponse(.failure(.unknown))

var error: Error?
do {
await #expect(throws: TMDbError.unknown) {
_ = try await service.details(forCompany: companyID)
} catch let err {
error = err
}

let tmdbAPIError = try XCTUnwrap(error as? TMDbError)

XCTAssertEqual(tmdbAPIError, .unknown)
}

}
1 change: 1 addition & 0 deletions Tests/TMDbTests/TestUtils/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ extension Tag {
@Tag static var account: Self
@Tag static var authentication: Self
@Tag static var certification: Self
@Tag static var company: Self

}

0 comments on commit 44acc55

Please sign in to comment.