@@ -6,6 +6,7 @@ @implementation PetApiTest
6
6
- (void )setUp {
7
7
[super setUp ];
8
8
api = [[SWGPetApi alloc ]init];
9
+ // [[SWGApiClient sharedClientFromPool]setLoggingEnabled:true];
9
10
[SWGPetApi setBasePath: @" http://localhost:8002/api" ];
10
11
}
11
12
@@ -14,17 +15,22 @@ - (void)tearDown {
14
15
}
15
16
16
17
- (void )testGetPetById {
18
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testGetPetById" ];
17
19
[api getPetByIdWithCompletionBlock: @1 completionHandler: ^(SWGPet *output, NSError *error) {
18
20
if (error){
19
21
XCTFail (@" got error %@ " , error);
20
22
}
21
23
if (output){
22
24
XCTAssertNotNil ([output _id ], @" token was nil" );
23
25
}
26
+ [expectation fulfill ];
24
27
}];
28
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
25
29
}
26
30
27
31
- (void ) testAddPet {
32
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testAddPet" ];
33
+
28
34
SWGPet * petToAdd = [[SWGPet alloc ] init ];
29
35
[petToAdd set_id: @1000 ];
30
36
NSMutableArray * tags = [[NSMutableArray alloc ] init ];
@@ -53,10 +59,14 @@ - (void) testAddPet {
53
59
if (error){
54
60
XCTFail (@" got error %@ " , error);
55
61
}
62
+ [expectation fulfill ];
56
63
}];
64
+
65
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
57
66
}
58
67
59
68
- (void ) testUpdatePet {
69
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testUpdatePet" ];
60
70
SWGPet * petToAdd = [[SWGPet alloc ] init ];
61
71
[petToAdd set_id: [NSNumber numberWithInt: 1000 ]];
62
72
NSMutableArray * tags = [[NSMutableArray alloc ] init ];
@@ -80,7 +90,8 @@ - (void) testUpdatePet {
80
90
[photos addObject: url];
81
91
}
82
92
[petToAdd setPhotoUrls: photos];
83
-
93
+ dispatch_semaphore_t sema = dispatch_semaphore_create (0 );
94
+
84
95
static bool hasResponse = false ;
85
96
[api addPetWithCompletionBlock: petToAdd completionHandler: ^(NSError *error) {
86
97
if (error) {
@@ -119,16 +130,19 @@ - (void) testUpdatePet {
119
130
XCTAssertEqualObjects ([pet name ], @" programmer" , @" pet name was not updated" );
120
131
XCTAssertEqualObjects ([pet status ], @" confused" , @" pet status was not updated" );
121
132
}
133
+ [expectation fulfill ];
134
+
122
135
}];
123
136
}];
124
137
}
125
138
}];
126
139
}
127
140
}];
141
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
128
142
}
129
143
130
144
- (void )testGetPetByStatus {
131
- bool done = false ;
145
+ XCTestExpectation *expectation = [ self expectationWithDescription: @" testGetPetByStatus " ] ;
132
146
static NSMutableArray * pets = nil ;
133
147
static NSError * gError = nil ;
134
148
[api findPetsByStatusWithCompletionBlock: @" available" completionHandler: ^(NSArray *output, NSError *error) {
@@ -145,22 +159,11 @@ - (void)testGetPetByStatus {
145
159
}
146
160
}
147
161
}];
148
- NSDate * loopUntil = [NSDate dateWithTimeIntervalSinceNow: 10 ];
149
- while (!done && [loopUntil timeIntervalSinceNow ] > 0 ){
150
- if (gError ){
151
- XCTFail (@" got error %@ " , gError );
152
- done = true ;
153
- }
154
- if (pets){
155
- for (SWGPet * pet in pets) {
156
- XCTAssertEqualObjects ([pet status ], @" available" , @" got invalid status for pets" );
157
- }
158
- done = true ;
159
- }
160
- }
162
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
161
163
}
162
164
163
165
- (void )testGetPetByTags {
166
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testGetPetByTags" ];
164
167
[api findPetsByTagsWithCompletionBlock: @" tag1,tag2" completionHandler: ^(NSArray *output, NSError *error) {
165
168
if (error){
166
169
XCTFail (@" got error %@ " , error);
@@ -176,6 +179,8 @@ - (void)testGetPetByTags {
176
179
XCTFail (@" failed to find tag in pet" );
177
180
}
178
181
}
182
+ [expectation fulfill ];
179
183
}];
184
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
180
185
}
181
186
@end
0 commit comments