首页 > 解决方案 > 迁移到 Xcode 10 后单元测试失败

问题描述

我有一套在 Xcode 9.4.1 上运行良好的应用程序单元测试。升级到 10.1 后,我在将其指向的相应 var 设置为 nil 后断言弱引用 var 为 nil 时遇到了失败。任何想法为什么现在我已经升级了这会失败?其他测试遵循这种模式,但没有失败。编辑:这是新的非失败代码。只需要在 performSetup() autoreleasepool{} 中初始化按钮...

var button: CustomButton!
var buttonSize: CGSize = CGSize(width: 0, height: 0)

override func performSetup(file: StaticString = #file, line: UInt = #line) {
    super.performSetup(file: file, line: line)


    autoreleasepool {
        if button == nil {
            button = CustomButton()
            buttonSize = button.designDataMinimumButtonSize()
        }
        addToAndCenterWithinHostView(subview: button)
    }
}

override func performTeardown(file: StaticString = #file, line: UInt = #line) {
    // Verify that no strong reference cycles keep the test objects alive.
    weak var weakComponent = button

    autoreleasepool {
        button.removeFromSuperview()
        button = nil

        super.performTeardown(file: file, line: line)
    }

    // This assertion tests for strong reference cycles in the tested code.
    // However, if the assertion fails, it may be because the test did not
    // wait for a closure that was asynchronously dispatched to finish. In
    // that case, there is not a bug in the tested code and the test should
    // be modified to wait for the dispatched closure to finish.
    XCTAssertNil(weakComponent, "Expected no strong references to 'button' to exist.")
}

标签: iosswiftxcode

解决方案


Solved this by putting the initialization of the button in the performSetup() method's autoreleasepool{}. Answer edited with new code...


推荐阅读