Skip to content

Instantly share code, notes, and snippets.

@mdb1
Created February 3, 2023 19:44
Show Gist options
  • Save mdb1/938f05113a0fe384197a798c95096284 to your computer and use it in GitHub Desktop.
Save mdb1/938f05113a0fe384197a798c95096284 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