我正在尝试测试Diary
依赖于Network
.
所以Diary
代码:
- (PMKPromise *)saveAndUploadToServer:(DiaryItem *)item
{
return [self save:item].then(^{
return [self upload:item]; << See UPDATE //I put breakpoint here, it is never called
});
}
- (PMKPromise *)save:(DiaryItem *)item
{
return [PMKPromise new:^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject) {
[self.entryCreationManagedContext performBlock:^{
BOOL success;
NSError *saveError = nil;
item.status = @(UploadingStatus);
success = [self.entryCreationManagedContext save:&saveError];
if (success) {
fulfill(item.objectID);
}
else {
reject(saveError);
}
}];
}];
}
- (PMKPromise*)upload:(DiaryItem*)item
{
return [self.network POST:self.diaryUrl parameters:[item dictionary]].then(^{
return [self reportUploadAnalytics];
});
}
和测试:
- (void)testFailedUploadingReportsAnalytics
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Operations completed"];
[self uploadToServerAndReturnCallback].finally(^{
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
assertThat(error, is(nilValue()));
//check that mock called
}];
}
在这个Network
测试中是模拟的。但是我看到的承诺链没有被执行。它卡住了。可能是因为then:
在主线程上调用了块并且XCTest
正在暂停它。但同时它可能应该在 5 秒后继续。可能是什么问题?
更新
看起来这与我最初的假设无关。如果我替换[self.entryCreationManagedContext save:&saveError]
为YES
然后调试到达断点。
更新 2
托管上下文的这种特殊保存看起来像问题。它正在触发有关同步另一个托管上下文的通知。我们正在发现那里还有什么。