Skip to content

Instantly share code, notes, and snippets.

@vladimirgoncharov
Last active October 11, 2017 23:18
Show Gist options
  • Save vladimirgoncharov/22dc024c4204d77baaa8c6c15652087e to your computer and use it in GitHub Desktop.
Save vladimirgoncharov/22dc024c4204d77baaa8c6c15652087e to your computer and use it in GitHub Desktop.
Stub current time
// Works only `NSDate()`! `Date()` will not be stubbed. Uses a Private API and should not be in main project target or your application will be rejected !!!
extension NSDate {
func stubAsCurrenDate(block: () -> Void) {
let customDateBlock: @convention(block) (AnyObject) -> NSDate = { _ in self }
let implementation = imp_implementationWithBlock(unsafeBitCast(customDateBlock, to: AnyObject.self))
let method = class_getInstanceMethod(NSClassFromString("__NSPlaceholderDate"), #selector(NSObject.init))
let oldImplementation = method_getImplementation(method)
method_setImplementation(method, implementation)
block()
method_setImplementation(method, oldImplementation)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment