- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
最近我试图使用单元测试验证我编写的对象是否正确解除分配。然而,我发现无论我尝试什么,对象都不会在测试完成之前解除分配。因此,我将测试简化为一个简单的示例(见下文),它试图证明使用弱变量进行对象释放的基础知识。
在我看来,强引用应该在测试方法退出后停止保留对象,而弱引用应该在下一个运行循环引用时为nil。但是,弱引用永远不会为 nil,并且两个测试都失败了。我在这里误解了什么吗?以下是完整的单元测试。
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 可能以某种方式推迟了释放,但即使将测试方法主体包装在 autoreleasepool
中也不会导致对象释放。
最佳答案
问题是当调用 dispatchAsync
block 时,您的 testWeakVarDeallocation()
函数还没有退出,所以对 strongMock
的强引用是仍然持有。
像这样尝试(允许 testWeakVarDeallocation()
退出),你会看到 weakMock
变成了预期的 nil
:
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")
}
}
关于 swift XCTest : Verify proper deallocation of weak variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41308381/
我有一个使用 XCTest 框架的 CocoaPod。为了将 XCTest 导入 pod,我将 s.frameworks = 'XCTest' 添加到 .podspec 文件。 我现在可以将 XCTe
我正在尝试在 iphone 设备上运行 XCTest,但 Xcode 显示以下消息。 "Logic Testing on iOS devices is not supported. You can r
我认为,我在那里找到的关于主题的所有信息都是有用的信息,但我需要了解更小的步骤。我如何创建测试,如何单击并滑动浏览应用程序中的一些文章,如何测试长按手势,..请提供一些有助于初学者的小示例,谢谢。 最
我正在尝试在 Xcode 中开始使用 tdd,并遵循了一个非常好的 tutorial . 一切顺利。但是,当我想在 Xcode 中创建命令行应用程序(在 c 中)项目时,未创建单独的测试文件夹。 我想
我使用查找器将一些图像(选项卡图标)添加到我的应用程序的目录中,然后使用文件添加将它们添加到项目中,最后在 images.xcassets 中添加图像集,将它们拖到上方以用作选项卡图标。 不知何故,在
在重命名之前,我首先复制了我的项目。重命名后我得到了 Lexical or Preprocessor Issue not found. 错误将我引导至调用 的 myapptests.m 文件 #im
XCTest waitForExpectationsWithTimeout:handler: 的文档指出 Only one -waitForExpectationsWithTimeout:handle
从Xcode Beta 5在iOS 8上运行测试时因以下错误而崩溃: dyld: Symbol not found: _kAXXCAttributeAbsoluteValue Referenced
例如,在使用 icloud 数据填充本地数据的测试成功之前,我不想运行从我的本地数据中选择的函数的测试。 我认为一个合适的单元测试应该在测试本地数据选择方法之前编写一些预定义的本地数据(人们同意吗?)
尝试使用 cocoa pods 将库集成到我的项目时出现以下异常。 dyld:未加载库:@rpath/XCTest.framework/XCTest 引用自:/Users/shoaibahmedqur
我是 ios 世界的新手,正在弄清楚如何从命令行运行测试。 我有 APP.app 和 TEST.xctest(基本上是 KIF 测试)。 我无法弄清楚如何在应用程序(在模拟器和设备上)上运行这些测试。
我觉得我有一个很常见的问题,但不知何故错过了关于最佳实践的备忘录。我想弄清楚如何在 XCTest 中嵌套异步调用。 我正在尝试通过基于 Alamofire 的 REST API 测试/清理 XCTes
我有几个项目正在尝试使用 Xcode 6 Beta 2 构建。这些项目都有某种类型的库使用 XCTest(Kiwi/XCTest 和 Specta),这些库不能在 Xcode 6 中构建,因为 XCT
谁能给我解释一下这里发生了什么?我不明白这个错误是什么意思。 XCTest.framework/XCTest: no matching architecture in universal wrappe
我在此处关注有关使用钥匙串(keychain)的教程 http://code.tutsplus.com/tutorials/securing-and-encrypting-data-on-ios--m
我在Xcode 5.0.2中运行项目时遇到问题 我收到以下错误: dyld: Library not loaded: @rpath/XCTest.framework/Versions/A/XCTest
所以,这是错误,我记录了我的 UI 脚本,但是当我重放 UI 脚本时发生了错误。控制台显示: “dyld:未加载库:@rpath/XCTest.framework/XCTest 引用自:/var/co
我在 中导入了我的旧项目Xcode 6.1 并且上面提到的错误在运行时开始出现,原因是 图片未找到 ,我已经尝试了以下链接中建议的所有答案 Xcode 5.0.2 dyld error 但到目前为止,
在 OS X 10.9.5 中,我从 Xcode 5.1.1 升级到 Xcode 6.0.1(通过 App Store)。 现在在我的项目中 XCTest.framework和 MyAppTests.
在我们的 Jenkins CI slave 上的 iOS 应用程序上运行 UI 测试时,UI 不正确并且我的测试失败。在本地,相同的 UI 符合预期并且测试通过。 我一直无法弄清楚这一点,也无法像在远
我是一名优秀的程序员,十分优秀!