【发布时间】:2016-05-03 08:07:23
【问题描述】:
我有一项服务,目前正在为其编写单元测试。代码按预期工作,但我收到一个奇怪的保留周期警告。
[self.myService doSomethingCoolWithCompletionBlock:^(MyResponseObject *obj) {
XCTAssertNil(obj, @"obj should be nil");
}];
XCTAssertNil(obj, @"obj should be nil"); 行在 Xcode 中显示警告 Capturing 'self' strongly in this block is likely to lead to a retain cycle。
如果我将代码更改为以下内容,则会删除警告:
__weak MyService *weakService = self.myService;
[weakService doSomethingCoolWithCompletionBlock:^(MyResponseObject *obj) {
XCTAssertNil(obj, @"obj should be nil");
}];
我在其他单元测试中使用self.someService,从来没有遇到过这个问题。有没有人经历过这种情况?
编辑
我有另一个测试具有以下内容:
[self.myService doSomethingElseCoolWithCompletionBlock:(NSArray *results) {
XCTestAssertNotNil(results, @"results should not be nil");
}];
这并没有给我一个警告。我看到的唯一区别是这是检查数组,另一个是检查特定类型的对象。
【问题讨论】:
标签: ios objective-c xcode unit-testing