@@ -116,12 +116,11 @@ - (void)testRequestDatafileHandlesCompletionEvenWithBadRequest {
116
116
117
117
- (void )testSaveDatafileMethod {
118
118
// setup datafile manager and datastore
119
- OPTLYDataStore *dataStore = [OPTLYDataStore new ];
120
119
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock: ^(OPTLYDatafileManagerBuilder * _Nullable builder) {
121
120
builder.projectId = kProjectId ;
122
121
}];
123
122
XCTAssertNotNil (datafileManager);
124
- XCTAssertFalse ([dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile]);
123
+ XCTAssertFalse ([self . dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile]);
125
124
126
125
// get the datafile
127
126
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ];
@@ -130,10 +129,10 @@ - (void)testSaveDatafileMethod {
130
129
[datafileManager saveDatafile: datafile];
131
130
132
131
// test the datafile was saved correctly
133
- bool fileExists = [dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile];
132
+ bool fileExists = [self . dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile];
134
133
XCTAssertTrue (fileExists, @" save Datafile did not save the datafile to disk" );
135
134
NSError *error;
136
- NSData *savedData = [dataStore getFile: kProjectId
135
+ NSData *savedData = [self . dataStore getFile: kProjectId
137
136
type: OPTLYDataStoreDataTypeDatafile
138
137
error: &error];
139
138
XCTAssertNil (error);
@@ -143,11 +142,32 @@ - (void)testSaveDatafileMethod {
143
142
}
144
143
145
144
- (void )testDatafileManagerPullsDatafileOnInitialization {
146
- // setup stubbing
145
+ // setup stubbing and listener expectation
146
+ __weak XCTestExpectation *expectation = [self expectationWithDescription: @" testInitializeClientAsync" ];
147
+ id <OHHTTPStubsDescriptor> stub = [OHHTTPStubs stubRequestsPassingTest: ^BOOL (NSURLRequest *request) {
148
+ return [request.URL.host isEqualToString: @" cdn.optimizely.com" ];
149
+ } withStubResponse: ^OHHTTPStubsResponse*(NSURLRequest *request) {
150
+ // Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
151
+ [expectation fulfill ];
152
+ return [OHHTTPStubsResponse responseWithData: [OPTLYTestHelper loadJSONDatafileIntoDataObject: kDatamodelDatafileName ]
153
+ statusCode: 200
154
+ headers: @{@" Content-Type" :@" application/json" }];
155
+ }];
147
156
148
157
// instantiate datafile manager (it should fire off a request)
158
+ XCTAssertFalse ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" no datafile sould exist yet." );
159
+ OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock: ^(OPTLYDatafileManagerBuilder * _Nullable builder) {
160
+ builder.projectId = kProjectId ;
161
+ }];
162
+ XCTAssertNotNil (datafileManager);
149
163
164
+ // make sure we were able to save the datafile
165
+ [self waitForExpectationsWithTimeout: 2 handler: nil ];
166
+ sleep (2 ); // not sure if there is a better way for to wait for disk write other than to sleep this thread
167
+ XCTAssertTrue ([self .dataStore fileExists: kProjectId type: OPTLYDataStoreDataTypeDatafile], @" we should have stored the datafile" );
150
168
169
+ // cleanup stubs
170
+ [OHHTTPStubs removeStub: stub];
151
171
}
152
172
153
173
@end
0 commit comments