Skip to content

Commit b0f22e0

Browse files
authored
Merge pull request #19588 from wordpress-mobile/refactor-context-manager-mock
Remove ContextManagerMock
2 parents fcd3459 + 8136321 commit b0f22e0

19 files changed

+45
-62
lines changed

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@
11181118
4AD5656F28E413160054C676 /* Blog+History.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AD5656E28E413160054C676 /* Blog+History.swift */; };
11191119
4AD5657028E413160054C676 /* Blog+History.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AD5656E28E413160054C676 /* Blog+History.swift */; };
11201120
4AD5657228E543A30054C676 /* BlogQueryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AD5657128E543A30054C676 /* BlogQueryTests.swift */; };
1121-
4AFB8FBF2824999500A2F4B2 /* ContextManagerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AFB8FBE2824999400A2F4B2 /* ContextManagerMock.swift */; };
1121+
4AFB8FBF2824999500A2F4B2 /* ContextManager+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AFB8FBE2824999400A2F4B2 /* ContextManager+Helpers.swift */; };
11221122
4B2DD0F29CD6AC353C056D41 /* Pods_WordPressUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DCE7542239FBC709B90EA85 /* Pods_WordPressUITests.framework */; };
11231123
4BB2296498BE66D515E3D610 /* Pods_JetpackShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 23052F0F1F9B2503E33D0A26 /* Pods_JetpackShareExtension.framework */; };
11241124
4D520D4F22972BC9002F5924 /* acknowledgements.html in Resources */ = {isa = PBXBuildFile; fileRef = 4D520D4E22972BC9002F5924 /* acknowledgements.html */; };
@@ -6436,7 +6436,7 @@
64366436
4AD5656B28E3D0670054C676 /* ReaderPost+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ReaderPost+Helper.swift"; sourceTree = "<group>"; };
64376437
4AD5656E28E413160054C676 /* Blog+History.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Blog+History.swift"; sourceTree = "<group>"; };
64386438
4AD5657128E543A30054C676 /* BlogQueryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogQueryTests.swift; sourceTree = "<group>"; };
6439-
4AFB8FBE2824999400A2F4B2 /* ContextManagerMock.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContextManagerMock.swift; sourceTree = "<group>"; };
6439+
4AFB8FBE2824999400A2F4B2 /* ContextManager+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ContextManager+Helpers.swift"; sourceTree = "<group>"; };
64406440
4D520D4E22972BC9002F5924 /* acknowledgements.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = acknowledgements.html; path = "../Pods/Target Support Files/Pods-Apps-WordPress/acknowledgements.html"; sourceTree = "<group>"; };
64416441
4D670B9448DF9991366CF42D /* Pods_WordPressStatsWidgets.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WordPressStatsWidgets.framework; sourceTree = BUILT_PRODUCTS_DIR; };
64426442
51A5F017948878F7E26979A0 /* Pods-Apps-WordPress.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Apps-WordPress.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Apps-WordPress/Pods-Apps-WordPress.release.xcconfig"; sourceTree = "<group>"; };
@@ -15644,7 +15644,7 @@
1564415644
E180BD4D1FB4681E00D0D781 /* MockCookieJar.swift */,
1564515645
E1B642121EFA5113001DC6D7 /* ModelTestHelper.swift */,
1564615646
4A17C1A3281A823E0001FFE5 /* NSManagedObject+Fixture.swift */,
15647-
4AFB8FBE2824999400A2F4B2 /* ContextManagerMock.swift */,
15647+
4AFB8FBE2824999400A2F4B2 /* ContextManager+Helpers.swift */,
1564815648
933D1F451EA64108009FB462 /* TestingAppDelegate.h */,
1564915649
933D1F461EA64108009FB462 /* TestingAppDelegate.m */,
1565015650
933D1F6B1EA7A3AB009FB462 /* TestingMode.storyboard */,
@@ -22501,7 +22501,7 @@
2250122501
B532ACD31DC3AE1200FFFA57 /* OHHTTPStubs+Helpers.swift in Sources */,
2250222502
E11DF3E420C97F0A00C0B07C /* NotificationCenterObserveOnceTests.swift in Sources */,
2250322503
5960967F1CF7959300848496 /* PostTests.swift in Sources */,
22504-
4AFB8FBF2824999500A2F4B2 /* ContextManagerMock.swift in Sources */,
22504+
4AFB8FBF2824999500A2F4B2 /* ContextManager+Helpers.swift in Sources */,
2250522505
3F751D462491A93D0008A2B1 /* ZendeskUtilsTests+Plans.swift in Sources */,
2250622506
F44FB6CB287895AF0001E3CE /* SuggestionsListViewModelTests.swift in Sources */,
2250722507
8BD36E062395CC4400EFFF1C /* MediaEditorOperation+DescriptionTests.swift in Sources */,

WordPress/WordPressTest/Blog+ObjcTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#import "WordPressTest-Swift.h"
33

44
@interface Blog_ObjcTests : XCTestCase
5-
@property (strong, nonatomic) ContextManagerMock *contextManager;
5+
@property (strong, nonatomic) ContextManager *contextManager;
66
@end
77

88
@implementation Blog_ObjcTests
99

1010
- (void)setUp {
11-
self.contextManager = [ContextManagerMock new];
11+
self.contextManager = [ContextManager forTesting];
1212
[super setUp];
1313
}
1414

WordPress/WordPressTest/BlogJetpackTest.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ @interface BlogJetpackTest ()
1717

1818
@property (nonatomic, strong) WPAccount *account;
1919
@property (nonatomic, strong) Blog *blog;
20-
@property (nonatomic, strong) ContextManagerMock *testContextManager;
20+
@property (nonatomic, strong) ContextManager *testContextManager;
2121
@end
2222

2323
@implementation BlogJetpackTest
2424

2525
- (void)setUp {
2626
[super setUp];
27-
self.testContextManager = [[ContextManagerMock alloc] init];
28-
[self.testContextManager useAsSharedInstanceUntilTestFinished:self];
27+
self.testContextManager = [ContextManager forTesting];
2928

3029
_blog = (Blog *)[NSEntityDescription insertNewObjectForEntityForName:@"Blog"
3130
inManagedObjectContext:self.testContextManager.mainContext];

WordPress/WordPressTest/BlogServiceTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ @interface BlogServiceTest : XCTestCase
1212
@property (nonatomic, strong) BlogService *blogService;
1313
@property (nonatomic, strong) id blogServiceMock;
1414
@property (nonatomic, strong) Blog *blog;
15-
@property (nonatomic, strong) ContextManagerMock *coreDataStack;
15+
@property (nonatomic, strong) ContextManager *coreDataStack;
1616

1717
@end
1818

@@ -22,7 +22,7 @@ - (void)setUp
2222
{
2323
[super setUp];
2424

25-
self.coreDataStack = [[ContextManagerMock alloc] init];
25+
self.coreDataStack = [ContextManager forTesting];
2626

2727
self.blogService = [[BlogService alloc] initWithManagedObjectContext:[self.coreDataStack mainContext]];
2828
AccountService *service = [[AccountService alloc] initWithManagedObjectContext:self.coreDataStack.mainContext];

WordPress/WordPressTest/BlogSiteVisibilityHelperTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ @interface BlogSiteVisibilityHelperTest : XCTestCase
99
@end
1010

1111
@interface BlogSiteVisibilityHelperTest ()
12-
@property (nonatomic, strong) ContextManagerMock *coreDataStack;
12+
@property (nonatomic, strong) ContextManager *coreDataStack;
1313
@end
1414

1515
@implementation BlogSiteVisibilityHelperTest
@@ -18,7 +18,7 @@ - (void)setUp
1818
{
1919
[super setUp];
2020

21-
self.coreDataStack = [ContextManagerMock new];
21+
self.coreDataStack = [ContextManager forTesting];
2222
}
2323

2424
- (void)tearDown

WordPress/WordPressTest/BlogTimeZoneTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#import "WordPressTest-Swift.h"
66

77
@interface BlogTimeZoneTests : XCTestCase
8-
@property (nonatomic, strong) ContextManagerMock *coreDataStack;
8+
@property (nonatomic, strong) ContextManager *coreDataStack;
99
@property (nonatomic, strong) Blog *blog;
1010
@end
1111

@@ -15,7 +15,7 @@ - (void)setUp
1515
{
1616
[super setUp];
1717

18-
self.coreDataStack = [[ContextManagerMock alloc] init];
18+
self.coreDataStack = [ContextManager forTesting];
1919

2020
AccountService *service = [[AccountService alloc] initWithManagedObjectContext: self.coreDataStack.mainContext];
2121
WPAccount *account = [service createOrUpdateAccountWithUsername:@"test" authToken:@"token"];

WordPress/WordPressTest/BloggingRemindersSchedulerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class BloggingRemindersSchedulerTests: XCTestCase {
5454
let schedule = BloggingRemindersScheduler.Schedule.weekdays(days)
5555
let store: BloggingRemindersStore
5656

57-
let contextManager = ContextManagerMock()
57+
let contextManager = ContextManager.forTesting()
5858
let context = contextManager.mainContext
5959
let blog = BlogBuilder(context).build()
6060

@@ -93,7 +93,7 @@ class BloggingRemindersSchedulerTests: XCTestCase {
9393
let cancelExpectation = expectation(description: "The notification is cancelled")
9494
cancelExpectation.expectedFulfillmentCount = days.count
9595

96-
let contextManager = ContextManagerMock()
96+
let contextManager = ContextManager.forTesting()
9797
let context = contextManager.mainContext
9898
let blog = BlogBuilder(context).build()
9999

WordPress/WordPressTest/ContextManagerMock.swift renamed to WordPress/WordPressTest/ContextManager+Helpers.swift

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,14 @@ import XCTest
22

33
@testable import WordPress
44

5-
class ContextManagerMock: ContextManager {
5+
extension ContextManager {
66

7-
/// Create an in memory database using the current version of the data model.
8-
convenience init() {
9-
self.init(modelName: ContextManagerModelNameCurrent)
10-
}
11-
12-
/// Create an in memory database using given `modelName`.
7+
/// Create an in memory database.
138
///
149
/// - SeeAlso `ContextManager`
15-
init(modelName: String) {
16-
super.init(modelName: modelName, store: ContextManager.inMemoryStoreURL, contextFactory: nil)
17-
}
18-
19-
override func saveContextAndWait(_ context: NSManagedObjectContext) {
20-
super.saveContextAndWait(context)
21-
// FIXME: Remove this method to use superclass one instead
22-
//
23-
// This log resolves a deadlock in
24-
// `DashboardCardTests.testShouldNotShowQuickStartIfDefaultSectionIsSiteMenu`
25-
//
26-
// Unfortunately, we're not sure why.
27-
NSLog("Context save completed")
10+
@objc
11+
static func forTesting() -> ContextManager {
12+
ContextManager(modelName: ContextManagerModelNameCurrent, store: ContextManager.inMemoryStoreURL, contextFactory: nil)
2813
}
2914

3015
/// Override `ContextManager.shared` with the mock instance, and un-override it when
@@ -34,7 +19,7 @@ class ContextManagerMock: ContextManager {
3419
/// `setUp` method.
3520
///
3621
/// - Parameter testCase: The test case to wait for.
37-
@objc func useAsSharedInstance(untilTestFinished testCase: XCTestCase) {
22+
func useAsSharedInstance(untilTestFinished testCase: XCTestCase) {
3823
// Create the test observer singleton to add it to `XCTestObservationCenter`.
3924
_ = AutomaticTeardownTestObserver.instance
4025

WordPress/WordPressTest/ContextManagerTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class ContextManagerTests: XCTestCase {
142142
}
143143

144144
func testSaveDerivedContextWithChangesInMainContext() throws {
145-
let contextManager = ContextManagerMock()
145+
let contextManager = ContextManager.forTesting()
146146
let derivedContext = contextManager.newDerivedContext()
147147

148148
derivedContext.perform {
@@ -179,15 +179,14 @@ class ContextManagerTests: XCTestCase {
179179
// It's not great to be accessing global state here, but ContextManager accesses this
180180
// feature flag under the hood and injecting it is out of scope at this point in time.
181181
if FeatureFlag.newCoreDataContext.enabled == false {
182-
// ContextManagerMock subclasses ContextManager, which uses LegacyContextFactory unless
183-
// the newCoreDataContext feature flag is on.
182+
// ContextManager uses LegacyContextFactory unless the newCoreDataContext feature flag is on.
184183
XCTExpectFailure("Known issue of `LegacyContextFactory`: the `mainContext` is saved along with the `ContextManager.save` functions")
185184
}
186185
expect(try findFirstUser()?.username) == "First User"
187186
}
188187

189188
func testSaveUsingBlock() async {
190-
let contextManager = ContextManagerMock()
189+
let contextManager = ContextManager.forTesting()
191190
let numberOfAccounts: () -> Int = {
192191
contextManager.mainContext.countObjects(ofType: WPAccount.self)
193192
}
@@ -233,7 +232,7 @@ class ContextManagerTests: XCTestCase {
233232
}
234233

235234
func testSaveUsingBlockWithNestedCalls() {
236-
let contextManager = ContextManagerMock()
235+
let contextManager = ContextManager.forTesting()
237236
let accounts: () -> Set<String> = {
238237
let all = (try? contextManager.mainContext.fetch(NSFetchRequest<WPAccount>(entityName: "Account"))) ?? []
239238
return Set(all.map { $0.username! })

WordPress/WordPressTest/CoreDataTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import XCTest
44
/// from this class to use the `CoreDataStack` mock instance in your test case.
55
class CoreDataTestCase: XCTestCase {
66

7-
private(set) lazy var contextManager: ContextManagerMock = {
8-
ContextManagerMock()
7+
private(set) lazy var contextManager: ContextManager = {
8+
ContextManager.forTesting()
99
}()
1010

1111
var mainContext: NSManagedObjectContext {

0 commit comments

Comments
 (0)