我一直在使用该XCTest
框架编写 Xcode 测试,主要是使用以下格式的完成处理程序对服务的 getter 进行异步测试,没有任何问题:
XCTestExpectation *promise = [self expectationWithDescription:@"Get Something should succeed"];
[self.myService getSomethingOnCompletion:^(NSError * _Nullable error) {
XCTAssertNil(error, @"Error should be nil");
[promise fulfill];
}];
[self waitForExpectations:@[promise] timeout:2.0];
今天突然间,我开始编写以下格式的第一个异步 setter 测试,但XCTAssert...()
在块内的语句中收到警告:
在此块中强烈捕获“自我”可能会导致保留周期
XCTestExpectation *promise = [self expectationWithDescription:@"Set Something should succeed"];
[self.myService setSomething:@"..." onCompletion:^(NSError * _Nullable error) {
XCTAssertNil(error, @"Error should be nil");
[promise fulfill];
}];
[self waitForExpectations:@[promise] timeout:2.0];
我什至不遗余力地注释掉它的全部内容setSomething: onCompletion:
,使其不做任何事情,清理和重建,但警告仍然存在。
我不明白self
它指的是什么,因为块内唯一发生的事情是XCTAssert...()
and [XCTestExpectation fulfill]
。此外,我不明白为什么我为第一种格式编写的 30 多个测试中没有一个没有与之相关的警告,但我为第二种格式编写的所有 5 多个测试都有。
任何关于这里发生的事情以及我如何解决它的解释将不胜感激。
(使用 Xcode 10.0)
编辑1:
问题似乎与方法名称有关,setSomething: onCompletion:
. 将其更改为其他任何内容,例如doSomething: onCompletion:
删除警告。我仍然不知道Xcode如何/为什么以set
显示警告的方式解释命令,因此任何信息都将不胜感激。
编辑2:
以下是 和 的方法setSomething
签名doSomething
:
- (void)setSomething:(EnumType)type onCompletion:(SetSomethingCompletionHandler)completion;
- (void)doSomething:(EnumType)type onCompletion:(SetSomethingCompletionHandler)completion
其中SetSomethingCompletionHandler
定义为:
typedef void (^SetSomethingCompletionHandler)(NSError * _Nullable error);