Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Stop using NSInvocation with OCMock's .andDo() #667

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions Source/santametricservice/SNTMetricServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,28 @@ - (void)testWritingJSONOverHTTP {
HTTPVersion:@"HTTP/1.1"
headerFields:@{@"content-type" : @"application/json"}];

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

__block dataTaskCompletion passedBlock;

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

// stub out session to call completion handler immediately.
void (^callCompletionHandler)(NSInvocation *) = ^(NSInvocation *invocation) {
passedBlock(nil, response, nil);
[responseCallback fulfill];
};

OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume]).andDo(callCompletionHandler);
OCMStub([(NSURLSessionDataTask *)self.mockSessionDataTask resume])
.andDo(^void(NSURLSessionDataTask *unused) {
passedBlock(nil, response, nil);
[responseCallback fulfill];
});

// stub out NSURLSession to assign our completion handler and return our mock
void (^getCompletionHandler)(NSInvocation *) = ^(NSInvocation *invocation) {
[invocation getArgument:&passedBlock atIndex:3];
};

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

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

__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);
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");
}
});

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

Expand Down