@@ -49,7 +49,8 @@ struct NetworkServiceMockCallback {
49
49
}
50
50
51
51
/**
52
- Mocks a `NetworkService`. You can configure expected results or errors to have a fully functional mock.
52
+ Mocks a `NetworkService`.
53
+ You can configure expected results or errors to have a fully functional mock.
53
54
54
55
**Example**:
55
56
```swift
@@ -58,12 +59,46 @@ struct NetworkServiceMockCallback {
58
59
let resource: Resource<String> = //
59
60
60
61
//When
61
- // Your test code
62
+ networkService.request(
63
+ resource,
64
+ onCompletion: { string in /*...*/ },
65
+ onError: { error in /*...*/ }
66
+ )
62
67
networkService.returnSuccess(with: "Sucess")
63
68
64
69
//Then
65
70
//Test your expectations
66
71
72
+ ```
73
+
74
+ It is possible to start multiple requests at a time.
75
+ All requests and responses (or errors) are processed
76
+ in order they have been called. So, everything is serial.
77
+
78
+ **Example**:
79
+ ```swift
80
+ //Given
81
+ let networkServiceMock = NetworkServiceMock()
82
+ let resource: Resource<String> = //
83
+
84
+ //When
85
+ networkService.request(
86
+ resource,
87
+ onCompletion: { string in /* Success */ },
88
+ onError: { error in /*...*/ }
89
+ )
90
+ networkService.request(
91
+ resource,
92
+ onCompletion: { string in /*...*/ },
93
+ onError: { error in /*. cancel error .*/ }
94
+ )
95
+
96
+ networkService.returnSuccess(with: "Sucess")
97
+ networkService.returnError(with: .cancelled)
98
+
99
+ //Then
100
+ //Test your expectations
101
+
67
102
```
68
103
69
104
- seealso: `NetworkService`
@@ -76,11 +111,11 @@ public final class NetworkServiceMock: NetworkService {
76
111
/// Set this to hava a custom networktask returned by the mock
77
112
public var nextNetworkTask : NetworkTask ?
78
113
79
- private var callbacks : NetworkServiceMockCallback ?
114
+ private var callbacks : [ NetworkServiceMockCallback ] = [ ]
80
115
81
116
/// Creates an instace of `NetworkServiceMock`
82
117
public init ( ) { }
83
-
118
+
84
119
/**
85
120
Fetches a resource asynchronously from remote location. Execution of the requests starts immediately.
86
121
Execution happens on no specific queue. It dependes on the network access which queue is used.
@@ -93,9 +128,9 @@ public final class NetworkServiceMock: NetworkService {
93
128
let resource: Resource<String> = //
94
129
95
130
networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in
96
- print(htmlText, response)
131
+ print(htmlText, response)
97
132
}, onError: { error in
98
- // Handle errors
133
+ // Handle errors
99
134
})
100
135
```
101
136
@@ -108,11 +143,15 @@ public final class NetworkServiceMock: NetworkService {
108
143
*/
109
144
@discardableResult
110
145
public func request< Result> ( queue: DispatchQueue , resource: Resource < Result > , onCompletionWithResponse: @escaping ( Result , HTTPURLResponse ) -> Void ,
111
- onError: @escaping ( NetworkError ) -> Void ) -> NetworkTask {
112
-
146
+ onError: @escaping ( NetworkError ) -> Void ) -> NetworkTask {
147
+
113
148
lastRequest = resource. request
114
149
requestCount += 1
115
- callbacks = NetworkServiceMockCallback ( resource: resource, onCompletionWithResponse: onCompletionWithResponse, onError: onError)
150
+ callbacks. append ( NetworkServiceMockCallback (
151
+ resource: resource,
152
+ onCompletionWithResponse: onCompletionWithResponse,
153
+ onError: onError
154
+ ) )
116
155
117
156
return nextNetworkTask ?? NetworkTaskMock ( )
118
157
}
@@ -121,25 +160,44 @@ public final class NetworkServiceMock: NetworkService {
121
160
///
122
161
/// - Parameters:
123
162
/// - error: the error which gets passed to the caller
124
- /// - count: the count, how often the error accours. 1 by default
125
- public func returnError( with error: NetworkError , count: Int = 1 ) {
126
- for _ in 0 ..< count {
127
- callbacks? . onErrorCallback ? ( error)
163
+ public func returnError( with error: NetworkError ) {
164
+ callbacks. removeFirst ( ) . onErrorCallback ? ( error)
165
+ }
166
+
167
+ /// Will return an error to the current waiting request.
168
+ ///
169
+ /// - Parameters:
170
+ /// - error: the error which gets passed to the caller
171
+ /// - count: the count, how often the error occours.
172
+ @available ( * , deprecated, message: " Use returnError without count parameter instead. Multiple calls can be done manually. " )
173
+ public func returnError( with error: NetworkError , count: Int ) {
174
+ ( 0 ..< count) . forEach { _ in
175
+ precondition ( !callbacks. isEmpty, " There is no request left to return an error for. " )
176
+ returnError ( with: error)
128
177
}
129
- callbacks = nil
178
+ }
179
+
180
+ /// Will return a successful request, by using the given data as a server response.
181
+ ///
182
+ /// - Parameters:
183
+ /// - data: the mock response from the server.
184
+ /// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
185
+ public func returnSuccess( with data: Data , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) ) {
186
+ callbacks. removeFirst ( ) . onSuccess ? ( data, httpResponse)
130
187
}
131
188
132
189
/// Will return a successful request, by using the given data as a server response.
133
190
///
134
191
/// - Parameters:
135
192
/// - data: the mock response from the server. `Data()` by default
136
193
/// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
137
- /// - count: the count how often the response gets triggerd. 1 by default
194
+ /// - count: the count how often the response gets triggerd.
195
+ @available ( * , deprecated, message: " Use returnSuccess without count parameter instead. Multiple calls can be done manually. " )
138
196
public func returnSuccess( with data: Data = Data ( ) , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) , count: Int = 1 ) {
139
- for _ in 0 ..< count {
140
- callbacks? . onSuccess ? ( data, httpResponse)
197
+ ( 0 ..< count) . forEach { _ in
198
+ precondition ( !callbacks. isEmpty, " There is no request left to return a success for. " )
199
+ returnSuccess ( with: data, httpResponse: httpResponse)
141
200
}
142
- callbacks = nil
143
201
}
144
202
145
203
/// Will return a successful request, by using the given type `T` as serialized result of a request.
@@ -149,12 +207,24 @@ public final class NetworkServiceMock: NetworkService {
149
207
/// - Parameters:
150
208
/// - data: the mock response from the server. `Data()` by default
151
209
/// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
152
- /// - count: the count how often the response gets triggerd. 1 by default
153
- public func returnSuccess< T> ( with serializedResponse: T , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) , count: Int = 1 ) {
154
- for _ in 0 ..< count {
155
- callbacks? . onTypedSuccess ? ( serializedResponse, httpResponse)
210
+ public func returnSuccess< T> ( with serializedResponse: T , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) ) {
211
+ callbacks. removeFirst ( ) . onTypedSuccess ? ( serializedResponse, httpResponse)
212
+ }
213
+
214
+ /// Will return a successful request, by using the given type `T` as serialized result of a request.
215
+ ///
216
+ /// **Warning:** This will crash if type `T` does not match your expected ResponseType of your current request
217
+ ///
218
+ /// - Parameters:
219
+ /// - data: the mock response from the server. `Data()` by default
220
+ /// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
221
+ /// - count: the count how often the response gets triggerd.
222
+ @available ( * , deprecated, message: " Use returnSuccess without count parameter instead. Multiple calls can be done manually. " )
223
+ public func returnSuccess< T> ( with serializedResponse: T , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) , count: Int ) {
224
+ ( 0 ..< count) . forEach { _ in
225
+ precondition ( !callbacks. isEmpty, " There is no request left to return a typed success for. " )
226
+ returnSuccess ( with: serializedResponse, httpResponse: httpResponse)
156
227
}
157
- callbacks = nil
158
228
}
159
229
160
230
}
0 commit comments