Forked from mdb1/XCTestCase+MemoryDeallocation.swift
Created
February 14, 2023 13:32
-
-
Save byaruhaf/ce67c3627dac19bcd70f00555f8fe565 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