@@ -13,12 +13,11 @@ class PhotoListViewModelTests: XCTestCase {
13
13
14
14
var sut : PhotoListViewModel !
15
15
var mockAPIService : MockApiService !
16
-
16
+
17
17
override func setUp( ) {
18
18
super. setUp ( )
19
19
mockAPIService = MockApiService ( )
20
- sut = PhotoListViewModel ( apiService: mockAPIService )
21
-
20
+ sut = PhotoListViewModel ( apiService: mockAPIService)
22
21
}
23
22
24
23
override func tearDown( ) {
@@ -27,7 +26,7 @@ class PhotoListViewModelTests: XCTestCase {
27
26
super. tearDown ( )
28
27
}
29
28
30
- func test_fetch_popular_photo_when_view_is_ready ( ) {
29
+ func test_fetch_photo ( ) {
31
30
// Given
32
31
mockAPIService. completePhotos = [ Photo] ( )
33
32
@@ -38,10 +37,9 @@ class PhotoListViewModelTests: XCTestCase {
38
37
XCTAssert ( mockAPIService!. isFetchPopularPhotoCalled)
39
38
}
40
39
41
- func test_fetch_fail ( ) {
40
+ func test_fetch_photo_fail ( ) {
42
41
43
42
// Given a failed fetch with a certain failure
44
- mockAPIService. completePhotos = nil
45
43
let error = APIError . permissionDenied
46
44
47
45
// When
@@ -56,7 +54,7 @@ class PhotoListViewModelTests: XCTestCase {
56
54
57
55
func test_create_cell_view_model( ) {
58
56
// Given
59
- let photos = PhotoStub ( ) . stubPhotos ( )
57
+ let photos = StubGenerator ( ) . stubPhotos ( )
60
58
mockAPIService. completePhotos = photos
61
59
let expect = XCTestExpectation ( description: " reload closure triggered " )
62
60
sut. reloadTableViewClosure = { ( ) in
@@ -102,7 +100,7 @@ class PhotoListViewModelTests: XCTestCase {
102
100
103
101
//Given a sut with fetched photos
104
102
let indexPath = IndexPath ( row: 0 , section: 0 )
105
- sutFinishedFetchPhotos ( )
103
+ goToFetchPhotoFinished ( )
106
104
107
105
//When
108
106
sut. userPressed ( at: indexPath )
@@ -117,7 +115,7 @@ class PhotoListViewModelTests: XCTestCase {
117
115
118
116
//Given a sut with fetched photos
119
117
let indexPath = IndexPath ( row: 4 , section: 0 )
120
- sutFinishedFetchPhotos ( )
118
+ goToFetchPhotoFinished ( )
121
119
122
120
let expect = XCTestExpectation ( description: " Alert message is shown " )
123
121
sut. showAlertClosure = { [ weak sut] in
@@ -138,10 +136,10 @@ class PhotoListViewModelTests: XCTestCase {
138
136
func test_get_cell_view_model( ) {
139
137
140
138
//Given a sut with fetched photos
141
- sutFinishedFetchPhotos ( )
139
+ goToFetchPhotoFinished ( )
142
140
143
141
let indexPath = IndexPath ( row: 1 , section: 0 )
144
- let testPhoto = mockAPIService. completePhotos! [ indexPath. row]
142
+ let testPhoto = mockAPIService. completePhotos [ indexPath. row]
145
143
146
144
// When
147
145
let vm = sut. getCellViewModel ( at: indexPath)
@@ -184,22 +182,21 @@ class PhotoListViewModelTests: XCTestCase {
184
182
185
183
}
186
184
185
+ //MARK: State control
187
186
extension PhotoListViewModelTests {
188
- private func sutFinishedFetchPhotos ( ) {
189
- mockAPIService. completePhotos = PhotoStub ( ) . stubPhotos ( )
187
+ private func goToFetchPhotoFinished ( ) {
188
+ mockAPIService. completePhotos = StubGenerator ( ) . stubPhotos ( )
190
189
sut. initFetch ( )
191
190
mockAPIService. fetchSuccess ( )
192
191
}
193
192
}
194
193
195
-
196
-
197
194
class MockApiService : APIServiceProtocol {
198
195
199
196
var isFetchPopularPhotoCalled = false
200
197
201
- var completePhotos : [ Photo ] ?
202
- var completeClosure : ( ( Bool , [ Photo ] , APIError ? ) -> ( ) ) ?
198
+ var completePhotos : [ Photo ] = [ Photo ] ( )
199
+ var completeClosure : ( ( Bool , [ Photo ] , APIError ? ) -> ( ) ) !
203
200
204
201
func fetchPopularPhoto( complete: @escaping ( Bool , [ Photo ] , APIError ? ) -> ( ) ) {
205
202
isFetchPopularPhotoCalled = true
@@ -208,16 +205,16 @@ class MockApiService: APIServiceProtocol {
208
205
}
209
206
210
207
func fetchSuccess( ) {
211
- completeClosure! ( true , completePhotos ?? [ Photo ] ( ) , nil )
208
+ completeClosure ( true , completePhotos, nil )
212
209
}
213
210
214
211
func fetchFail( error: APIError ? ) {
215
- completeClosure! ( false , completePhotos ?? [ Photo ] ( ) , error )
212
+ completeClosure ( false , completePhotos, error )
216
213
}
217
214
218
215
}
219
216
220
- class PhotoStub {
217
+ class StubGenerator {
221
218
func stubPhotos( ) -> [ Photo ] {
222
219
let path = Bundle . main. path ( forResource: " content " , ofType: " json " ) !
223
220
let data = try ! Data ( contentsOf: URL ( fileURLWithPath: path) )
0 commit comments