-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClearAllTests.swift
79 lines (68 loc) · 2.25 KB
/
ClearAllTests.swift
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
//
// ClearAllTests.swift
// StubTests
//
// Created by tokijh on 2022/04/18.
// Copyright © 2022 tokijh. All rights reserved.
//
import XCTest
import Stub
final class ClearAllTests: XCTestCase {
override func setUp() {
super.setUp()
Stub.clearAll()
}
func test_ClearAll() {
// given
let fetchServices = (0..<10).map { _ in FetchServiceStub() }
let saveServices = (0..<10).map { _ in SaveServiceStub() }
// Register and invoke to make executions and a stubbing function
fetchServices.forEach { fetchService in
fetchService.fetchNumberStub.register { completion in
completion(0)
}
fetchService.fetchNumber { _ in }
}
saveServices.forEach { saveService in
saveService.saveNumberStub.register { _ in }
saveService.saveNumber(number: 0)
}
let fetchNumberStubs = fetchServices.map(\.fetchNumberStub)
let saveNumberStubs = saveServices.map(\.saveNumberStub)
let fetchNumberExecutionsBeforeClearAll = fetchNumberStubs.flatMap { $0.executions() }
let saveNumberExecutionsBeforeClearAll = saveNumberStubs.flatMap { $0.executions() }
XCTAssertEqual(fetchNumberExecutionsBeforeClearAll.count, 10)
XCTAssertEqual(saveNumberExecutionsBeforeClearAll.count, 10)
fetchNumberStubs.forEach { stub in
XCTAssertTrue(stub.isStubbing)
}
saveNumberStubs.forEach { stub in
XCTAssertTrue(stub.isStubbing)
}
// when
Stub.clearAll()
// then
let fetchNumberExecutionsAfterClearAll = fetchNumberStubs.flatMap { $0.executions() }
let saveNumberExecutionsAfterClearAll = saveNumberStubs.flatMap { $0.executions() }
XCTAssertEqual(fetchNumberExecutionsAfterClearAll.count, 0)
XCTAssertEqual(saveNumberExecutionsAfterClearAll.count, 0)
fetchNumberStubs.forEach { stub in
XCTAssertFalse(stub.isStubbing)
}
saveNumberStubs.forEach { stub in
XCTAssertFalse(stub.isStubbing)
}
}
}
private final class FetchServiceStub {
@StubProxy(fetchNumber) var fetchNumberStub
func fetchNumber(_ completion: @escaping (Int) -> Void) {
fetchNumberStub.invoke(completion)
}
}
private final class SaveServiceStub {
@StubProxy(saveNumber) var saveNumberStub
func saveNumber(number: Int) {
saveNumberStub.invoke(number)
}
}