Skip to content

Commit

Permalink
Tests: Fix MetricServiceTest compatible with public OCMock
Browse files Browse the repository at this point in the history
  • Loading branch information
russellhancox committed Nov 8, 2021
1 parent ad6e03e commit 0a314cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
32 changes: 10 additions & 22 deletions Source/santametricservice/SNTMetricServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ - (void)setUp {

- (void)tearDown {
[self.mockConfigurator stopMocking];
if (self.mockSessionDataTask != nil) {
[self.mockSessionDataTask stopMocking];
}
if (self.mockSession != nil) {
[self.mockSession stopMocking];
}
if (self.mockMOLAuthenticatingURLSession != nil) {
[self.mockMOLAuthenticatingURLSession stopMocking];
}

// delete the temp dir
[[NSFileManager defaultManager] removeItemAtPath:self.tempDir error:NULL];
Expand Down Expand Up @@ -146,28 +137,25 @@ - (void)testWritingJSONOverHTTP {
HTTPVersion:@"HTTP/1.1"
headerFields:@{@"content-type" : @"application/json"}];

typedef void (^dataTaskCompletion)(NSData *, NSURLResponse *, NSError *);
typedef id (^completionHandler)(NSURLSession *, NSURLRequest *, dataTaskCompletion);

__block dataTaskCompletion passedBlock;
__unsafe_unretained __block void (^passedBlock)(NSData *, NSURLResponse *, NSError *);

XCTestExpectation *responseCallback =
[[XCTestExpectation alloc] initWithDescription:@"ensure writer passed JSON"];

// stub out session to call completion handler immediately.
OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume])
.andDo(^void(NSURLSessionDataTask *unused) {
OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume]).andDo(^(NSInvocation *inv) {
if (passedBlock) {
passedBlock(nil, response, nil);
[responseCallback fulfill];
});
}
[responseCallback fulfill];
});

// stub out NSURLSession to assign our completion handler and return our mock
OCMStub([self.mockSession dataTaskWithRequest:[OCMArg any] completionHandler:[OCMArg any]])
.andDo(
^id(NSURLSession *localSelf, NSURLRequest *request, dataTaskCompletion completionHandler) {
passedBlock = completionHandler;
return nil;
})
.andDo(^(NSInvocation *inv) {
[inv retainArguments];
[inv getArgument:&passedBlock atIndex:3];
})
.andReturn(self.mockSessionDataTask);

SNTMetricService *service = [[SNTMetricService alloc] init];
Expand Down
37 changes: 19 additions & 18 deletions Source/santametricservice/Writers/SNTMetricHTTPWriterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@ - (void)setUp {
self.httpWriter = [[SNTMetricHTTPWriter alloc] init];
self.mockResponses = [[NSMutableArray alloc] init];

typedef void (^dataTaskCompletion)(NSData *, NSURLResponse *, NSError *);
__block dataTaskCompletion completionHandler;

OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume])
.andDo(^void(NSURLSessionDataTask *unused) {
NSDictionary *responseValue = self.mockResponses[0];
if (responseValue != nil) {
completionHandler(responseValue[@"data"], responseValue[@"response"],
responseValue[@"error"]);
[self.mockResponses removeObjectAtIndex:0];
} else {
XCTFail(@"mockResponses set to zero");
}
});
__block void (^completionHandler)(NSData *, NSURLResponse *, NSError *);

void (^getCompletionHandler)(NSInvocation *) = ^(NSInvocation *invocation) {
[invocation getArgument:&completionHandler atIndex:3];
};

void (^callCompletionHandler)(NSInvocation *) = ^(NSInvocation *invocation) {
NSDictionary *responseValue = self.mockResponses[0];
if (responseValue != nil) {
completionHandler(responseValue[@"data"], responseValue[@"response"],
responseValue[@"error"]);
[self.mockResponses removeObjectAtIndex:0];
} else {
XCTFail(@"mockResponses set to zero");
}
};

OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume]).andDo(callCompletionHandler);

OCMStub([self.mockSession dataTaskWithRequest:[OCMArg any] completionHandler:[OCMArg any]])
.andDo(^id(NSURLSession *localSelf, NSURLRequest *request, dataTaskCompletion completion) {
completionHandler = completion;
return nil;
})
.andDo(getCompletionHandler)
.andReturn(self.mockSessionDataTask);
}

Expand Down

0 comments on commit 0a314cb

Please sign in to comment.