4

最近,我试图验证我编写的对象是否使用单元测试正确解除分配。然而,我发现无论我尝试什么,对象都不会在测试完成之前解除分配。所以我将测试简化为一个简单的例子(见下文),它试图证明使用弱变量的对象释放的基础知识。

在我看来,在测试方法退出后,强引用应该停止保留对象,而在下一个运行循环中引用时,弱引用应该为零。但是,弱引用永远不会为零,并且两个测试都失败了。我在这里误解了什么吗?以下是完整的单元测试。

class Mock { //class type, should behave with reference semantics

    init() { }
}

class DeallocationTests: XCTestCase {   

    func testWeakVarDeallocation() {   

        let strongMock = Mock()

        weak var weakMock: Mock? = strongMock

        let expt = expectation(description: "deallocated")

        DispatchQueue.main.async {

            XCTAssertNil(weakMock)      //This assertion fails

            expt.fulfill()
        }

        waitForExpectations(timeout: 1.0, handler: nil)
    }

    func testCaptureListDeallocation() {   

        let strongMock = Mock()

        let expt = expectation(description: "deallocated")

        DispatchQueue.main.async { [weak weakMock = strongMock] in

            XCTAssertNil(weakMock)      //This assertion also fails

            expt.fulfill()
        }

        waitForExpectations(timeout: 1.0, handler: nil)
    }
}

我认为 XCTest 可能会以某种方式推迟释放,但即使将测试方法体包装在 anautoreleasepool中也不会导致对象释放。

4

1 回答 1

4

问题是testWeakVarDeallocation()当块被调用时你的函数没有退出,所以仍然持有dispatchAsync强引用。strongMock

像这样尝试(允许testWeakVarDeallocation()退出),你会看到weakMocknil预期的那样:

class weakTestTests: XCTestCase {
    var strongMock: Mock? = Mock()

    func testWeakVarDeallocation() {
        weak var weakMock = strongMock

        print("weakMock is \(weakMock)")

        let expt = self.expectation(description: "deallocated")

        strongMock = nil

        print("weakMock is now \(weakMock)")

        DispatchQueue.main.async {
            XCTAssertNil(weakMock)      // This assertion fails

            print("fulfilling expectation")
            expt.fulfill()
        }

        print("waiting for expectation")
        self.waitForExpectations(timeout: 1.0, handler: nil)
        print("expectation fulfilled")
    }
}
于 2016-12-23T22:46:07.377 回答