Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Last active December 13, 2017 06:41
Show Gist options
  • Save levantAJ/839542deb121c4c644f87e81049050d2 to your computer and use it in GitHub Desktop.
Save levantAJ/839542deb121c4c644f87e81049050d2 to your computer and use it in GitHub Desktop.
- (void)applicationDidEnterBackground:(UIApplication *)application {
@weakify(self);
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
@strongify(self);
[application endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}
- (void)testApplicationDidEnterBackgroundExpirationHandler {
//Given:
NSUInteger identifier = 1;
__block void (^expirationHandler)(void);
OCMStub([self.application beginBackgroundTaskWithExpirationHandler:OCMOCK_ANY]).andDo(^(NSInvocation *invocation) {
void (^handler)(void);
[invocation getArgument:&handler atIndex:2];
expirationHandler = [handler copy];
// NSValue *returnValue = [NSValue valueWithBytes:&identifier objCType:@encode(NSUInteger)];
void *value = (void *)identifier;
[invocation setReturnValue:&value];
});
//When:
[self.appDelegate applicationDidEnterBackground:self.application];
expirationHandler();
//Then:
OCMVerify([self.application endBackgroundTask:identifier]);
XCTAssertEqual(self.appDelegate.backgroundTaskIdentifier, UIBackgroundTaskInvalid);
XCTAssertNotEqual(self.appDelegate.backgroundTaskIdentifier, identifier);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment