-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiClientTests.swift
More file actions
80 lines (67 loc) · 2.48 KB
/
ApiClientTests.swift
File metadata and controls
80 lines (67 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// ApiClientTests.swift
// BudgetTrackeriOSTests
//
// Created by Jonathan Wong on 7/20/19.
// Copyright © 2019 fatty waffles. All rights reserved.
//
import XCTest
@testable import BudgetTrackeriOS
class ApiClientTests: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testFetchManagers() {
let expect = expectation(description: "fetch managers")
let apiClient = ApiClient(session: MockNetworkSession(), resourcePath: "managers")
apiClient.fetchResources { (result: Result<[Manager], ApiError>) in
expect.fulfill()
switch result {
case .success(let managers):
XCTAssertNotNil(managers)
XCTAssertEqual(managers.count, 3)
case .failure(let error):
XCTAssertNil(error)
XCTFail("Could not fetch managers")
}
}
waitForExpectations(timeout: 2.0, handler: nil)
}
func testJSONError() {
let expect = expectation(description: "parsing JSON failed")
let mockNetworkErrorSession = MockNetworkErrorSession()
mockNetworkErrorSession.error = .jsonError
let apiClient = ApiClient(session: mockNetworkErrorSession, resourcePath: "managers")
apiClient.fetchResources { (result: Result<[Manager], ApiError>) in
expect.fulfill()
switch result {
case .success:
XCTFail()
case .failure(let error):
XCTAssertNotNil(error)
}
}
waitForExpectations(timeout: 2.0, handler: nil)
}
func testSaveTraining() {
let expect = expectation(description: "save training")
let apiClient = ApiClient(session:URLSession.shared, resourcePath: "trainings")
apiClient.save(fromPath: "trainings", fromResourceId: 1, toPath: "employees", toResourceId: 1) { statusCode in
expect.fulfill()
XCTAssertEqual(statusCode, 201)
}
waitForExpectations(timeout: 1.0, handler: nil)
}
func testDeleteTraining() {
let expect = expectation(description: "delete training")
let apiClient = ApiClient(session:URLSession.shared, resourcePath: "trainings")
apiClient.delete(fromPath: "trainings", fromResourceId: 1, toPath: "employees", toResourceId: 1) { statusCode in
expect.fulfill()
XCTAssertEqual(statusCode, 204)
}
waitForExpectations(timeout: 1.0, handler: nil)
}
}