-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRetainCycleTesting.swift
45 lines (40 loc) · 1.6 KB
/
RetainCycleTesting.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// The key thing to fixing retain cycles is detecting them.
// This tip looks at some code you can incorporate into
// your unit tests to help with the discovery of retain cycles.
// Function that allows us to validate that an object is correctly
// deallocated after we execute an arbitrary block of caller provided code.
// This will be useful for scenarios where you have an instance
// property that is holding onto your object.
extension XCTestCase {
func assertNil(_ subject: AnyObject?,
after: @escaping () -> Void,
file: StaticString = #file,
line: UInt = #line) {
guard let value = subject else {
return XCTFail("Argument must not be nil", file: file, line: line)
}
// Enqueuing a closure to be invoked after the test has been run
// and where our weak reference to our object is created
addTeardownBlock { [weak value] in
// where we execute our arbitrary closure
after()
//where we perform the assertion that our weak reference is nil'd out
XCTAssert(value == nil,
"Expected subject to be nil after test! Retain cycle?",
file: file,
line: line)
}
}
}
final class SampleTests: XCTestCase {
// instance property that is holding onto your object
var sut: Greeter!
override func setUp() {
super.setUp()
sut = Greeter()
assertNil(sut, after: { self.sut = nil })
}
func testGreeting() {
XCTAssertEqual(sut.greet("Paul"), "Hello Paul")
}
}