-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove "context saved" expectation from CoreDataStack mocks #18485
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,19 +75,17 @@ - (void)testJetpackUsername { | |
} | ||
|
||
- (void)testJetpackSetupDoesntReplaceDotcomAccount { | ||
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Context save expectation"]; | ||
self.testContextManager.testExpectation = saveExpectation; | ||
XCTestExpectation *saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil]; | ||
|
||
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext]; | ||
WPAccount *wpComAccount = [accountService createOrUpdateAccountWithUsername:@"user" authToken:@"token"]; | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
[self waitForExpectations:@[saveExpectation] timeout:2.0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this PR replaces all the What's your rationale for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this expectation was the only expectation created within this test method. This change is just making that explicit - test method is waiting for main context to be saved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. One could argue that using But, I'm with you on the value of explicitly stating which expectation we're waiting for. 👍 |
||
WPAccount * defaultAccount = [accountService defaultWordPressComAccount]; | ||
XCTAssertEqualObjects(wpComAccount, defaultAccount); | ||
|
||
saveExpectation = [self expectationWithDescription:@"Context save expectation"]; | ||
self.testContextManager.testExpectation = saveExpectation; | ||
saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil]; | ||
[accountService createOrUpdateAccountWithUsername:@"test1" authToken:@"token1"]; | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
[self waitForExpectations:@[saveExpectation] timeout:2.0]; | ||
defaultAccount = [accountService defaultWordPressComAccount]; | ||
XCTAssertEqualObjects(wpComAccount, defaultAccount); | ||
} | ||
|
@@ -101,8 +99,7 @@ - (void)testWPCCShouldntDuplicateBlogs { | |
statusCode:200 headers:@{@"Content-Type":@"application/json"}]; | ||
}]; | ||
|
||
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Context save expectation"]; | ||
self.testContextManager.testExpectation = saveExpectation; | ||
XCTestExpectation *saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil]; | ||
|
||
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; | ||
BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; | ||
|
@@ -130,7 +127,7 @@ - (void)testWPCCShouldntDuplicateBlogs { | |
}; | ||
|
||
// Wait on the merge to be completed | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
[self waitForExpectations:@[saveExpectation] timeout:2.0]; | ||
|
||
// test.blog + wp.com + jetpack | ||
XCTAssertEqual(1, [accountService numberOfAccounts]); | ||
|
@@ -174,8 +171,7 @@ - (void)testSyncBlogsMigratesJetpackSSL | |
statusCode:200 headers:@{@"Content-Type":@"application/json"}]; | ||
}]; | ||
|
||
XCTestExpectation *saveExpectation = [self expectationWithDescription:@"Context save expectation"]; | ||
self.testContextManager.testExpectation = saveExpectation; | ||
XCTestExpectation *saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil]; | ||
|
||
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; | ||
BlogService *blogService = [[BlogService alloc] initWithManagedObjectContext:self.testContextManager.mainContext]; | ||
|
@@ -192,7 +188,7 @@ - (void)testSyncBlogsMigratesJetpackSSL | |
jetpackBlog.url = @"https://jetpack.example.com/"; | ||
|
||
// Wait on the merge to be completed | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
[self waitForExpectations:@[saveExpectation] timeout:2.0]; | ||
|
||
XCTAssertEqual(1, [accountService numberOfAccounts]); | ||
// test.blog + wp.com + jetpack (legacy) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,6 @@ @implementation ContextManagerMock | |
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; | ||
@synthesize mainContext = _mainContext; | ||
@synthesize managedObjectModel = _managedObjectModel; | ||
@synthesize requiresTestExpectation = _requiresTestExpectation; | ||
@synthesize testExpectation = _testExpectation; | ||
|
||
- (instancetype)init | ||
{ | ||
|
@@ -23,7 +21,6 @@ - (instancetype)init | |
// Override the shared ContextManager | ||
[ContextManager internalSharedInstance]; | ||
[ContextManager overrideSharedInstance:self]; | ||
_requiresTestExpectation = YES; | ||
} | ||
|
||
return self; | ||
|
@@ -79,40 +76,13 @@ - (NSManagedObjectContext *)mainContext | |
return _mainContext; | ||
} | ||
|
||
- (void)saveContext:(NSManagedObjectContext *)context | ||
{ | ||
[self saveContext:context withCompletionBlock:^{ | ||
if (self.testExpectation) { | ||
[self.testExpectation fulfill]; | ||
self.testExpectation = nil; | ||
} else if (self.requiresTestExpectation) { | ||
NSLog(@"No test expectation present for context save"); | ||
} | ||
}]; | ||
} | ||
|
||
- (void)saveContextAndWait:(NSManagedObjectContext *)context | ||
{ | ||
[super saveContextAndWait:context]; | ||
if (self.testExpectation) { | ||
[self.testExpectation fulfill]; | ||
self.testExpectation = nil; | ||
} else if (self.requiresTestExpectation) { | ||
NSLog(@"No test expectation present for context save"); | ||
} | ||
} | ||
|
||
- (void)saveContext:(NSManagedObjectContext *)context withCompletionBlock:(void (^)(void))completionBlock | ||
{ | ||
[super saveContext:context withCompletionBlock:^{ | ||
if (self.testExpectation) { | ||
[self.testExpectation fulfill]; | ||
self.testExpectation = nil; | ||
} else if (self.requiresTestExpectation) { | ||
NSLog(@"No test expectation present for context save"); | ||
} | ||
completionBlock(); | ||
}]; | ||
// FIXME: Remove this method to use superclass one instead | ||
// This log magically resolves a deadlock in | ||
// `ZDashboardCardTests.testShouldNotShowQuickStartIfDefaultSectionIsSiteMenu` | ||
NSLog(@"Context save completed"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still trying to figure out cause of this deadlock 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what is worth, I can see the deadlock without this line on my end, too. The test you mention just sits there with a spinner next to it in the test navigator. |
||
} | ||
|
||
- (NSURL *)storeURL | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 it's surprising to see the expectation using
self.textContextManager.mainContext
and this using[ContextManager sharedInstance].mainContext
.They look like different contexts. Or am I missing something?
For reference, here's where each is instantiated (I think)
For
ContextManager
:WordPress-iOS/WordPress/Classes/Utility/ContextManager.m
Lines 72 to 83 in 112b352
For
TestContextManager
, which internally usesContextManager
:WordPress-iOS/WordPress/WordPressTest/ContextManagerMock.m
Lines 70 to 80 in 112b352
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! The code does look like they are referencing different context objects. But at runtime, they are actually the same one, the one in
ContextManagerMock
, since TestContextManager overrides the shared instance(the overriding actually happens withinContextManagerMock
). Which is whyself.testContextManager.mainContext
is same as[[ContextManager sharedInstance] mainContext]
.I will update the code to use the same reference for clarity.
BTW, the above overriding behaviour will be updated in my establishing usage pattern PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha! I knew I must have been missing something, else I would have expected the tests to fail 😄
Thank you for making it explicit in c6873f2. 👍