Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save byaruhaf/ce67c3627dac19bcd70f00555f8fe565 to your computer and use it in GitHub Desktop.
Save byaruhaf/ce67c3627dac19bcd70f00555f8fe565 to your computer and use it in GitHub Desktop.
assertMemoryDeallocation
import XCTest
public extension XCTestCase {
/// This is a method to determine that an instance gets deallocated from memory correctly; ie: no memory leaks 😄.
/// It leverages the `addTeardownBlock` method on `XCTest`:
/// `* Teardown blocks are executed after the current test method has returned but before tearDown is invoked.`
func assertMemoryDeallocation(
in instance: AnyObject,
file: StaticString = #filePath,
line: UInt = #line
) {
addTeardownBlock { [weak instance] in
XCTAssertNil(
instance,
"Instance should have been deallocated. Potential memory leak!",
file: file,
line: line
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment