ios - 迁移到 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.")
}
解决方案
Solved this by putting the initialization of the button in the performSetup() method's autoreleasepool{}. Answer edited with new code...
推荐阅读
- reactjs - 截取全日历时间网格视图的正确方法
- keystore - 如何应对异常“不是 JKS 或 JCEKS 密钥库(幻数错误;预期 FEEDFEED 或 CECECECE)”
- javascript - sinon.match() 不匹配对象与 Json.stringify 响应
- angular - 你将如何解决这个“承诺
' 不提供签名匹配 '(value: any): {} | PromiseLike<{}>" 错误 - c++ - 如何迭代没有开始/结束的容器?
- python-3.x - 通过 REST API 在 Google Cloud Platform 中更新 Cloud Armor 中的规则的 Python 脚本
- bash - 如果条件在 bash 脚本中为真,如何重新启动进程
- node.js - 如何从 JSON 创建 SOAP 请求正文?
- android - API 29 已弃用“getBitmap”。还有其他代码吗?
- reactjs - 如何在 ResultCard 中显示搜索结果中的详细信息页面?