@@ -149,29 +149,38 @@ - (void)testSaveDatafileMethod {
149
149
XCTAssertEqualObjects (datafile, savedData, @" retrieved saved data from disk should be equivilent to the datafile we wanted to save to disk" );
150
150
}
151
151
152
- - (void )testDatafileManagerPullsDatafileOnInitialization {
152
+ - (void )testDatafileManagerDownloadDatafileSavesDatafile {
153
+
154
+ // instantiate datafile manager
155
+ OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock: ^(OPTLYDatafileManagerBuilder * _Nullable builder) {
156
+ builder.projectId = kProjectId ;
157
+ }];
158
+ XCTAssertNotNil (datafileManager);
159
+ XCTAssertFalse ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" no datafile sould exist yet." );
160
+
153
161
// setup stubbing and listener expectation
154
162
__weak XCTestExpectation *expectation = [self expectationWithDescription: @" testInitializeClientAsync" ];
155
163
id <OHHTTPStubsDescriptor> stub = [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
156
164
return [request.URL.host isEqualToString: @" cdn.optimizely.com" ];
157
165
} withStubResponse: ^OHHTTPStubsResponse*(NSURLRequest *request) {
158
166
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
159
- [expectation fulfill ];
160
167
return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
161
168
statusCode: 200
162
169
headers: @{@" Content-Type" :@" application/json" }];
163
170
}];
164
171
165
- // instantiate datafile manager (it should fire off a request)
166
- XCTAssertFalse ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" no datafile sould exist yet." );
167
- OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock: ^(OPTLYDatafileManagerBuilder * _Nullable builder) {
168
- builder.projectId = kProjectId ;
172
+ // Call download datafile
173
+ __block Boolean completionWasCalled = false ;
174
+
175
+ [datafileManager downloadDatafile: datafileManager.projectId
176
+ completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
177
+ completionWasCalled = true ;
178
+ [expectation fulfill ];
169
179
}];
170
- XCTAssertNotNil (datafileManager);
171
180
172
181
// make sure we were able to save the datafile
173
182
[self waitForExpectationsWithTimeout: 2 handler: nil ];
174
- sleep ( 2 ); // not sure if there is a better way for to wait for disk write other than to sleep this thread
183
+ XCTAssertTrue (completionWasCalled);
175
184
XCTAssertTrue ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" we should have stored the datafile" );
176
185
177
186
// cleanup stubs
0 commit comments