Created
February 3, 2023 19:44
-
-
Save mdb1/938f05113a0fe384197a798c95096284 to your computer and use it in GitHub Desktop.
assertMemoryDeallocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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