在没有完成块的情况下测试异步方法?
我看到这个问题被问了几次,但他们的回答并没有真正回答这个问题。
我有一个与此类似的函数:
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
...
if ([NSThread isMainThread]) {
[self loadAttributedText];
} else {
@weakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self loadAttributedText];
});
}
}
return self;
}
我不必专门测试该函数,但我必须测试由函数 loadAttributedText
修改的某些类属性的值。
我面临的问题是,在类分配时,loadAttriubulatedText 被称为异步方法,因此当我尝试对类属性进行断言时,它们尚未初始化。
我尝试过使用 XCTestExpectation ,但在 initWithDictionary 方法中没有完成块,而且我不打算向其中添加一个完成块。
有没有办法测试这个?谢谢。
PS:我使用 OCMock 来编写测试。
I saw this question asked a few times but the responses they have doesn't really answer the question.
I have a function similar to this:
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
...
if ([NSThread isMainThread]) {
[self loadAttributedText];
} else {
@weakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self loadAttributedText];
});
}
}
return self;
}
I do not have to test specifically that function, but I do have to test the value of some class properties that are modified by the function loadAttributedText
.
The problem I'm facing is that at the time of class allocation, the loadAttriubutedText
is called an async method, so when I try to make an assertion on the class properties, they are not yet initialized.
I've tried using XCTestExpectation
but I do not have a completion block in the initWithDictionary
method and I am not intended to add one to it.
Is there a way to test this? Thanks.
P.S: I'm using OCMock to write the tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论