Skip to content

Commit 735cf3f

Browse files
authored
Remove sharedInstance from TextContextManager (#18512)
2 parents e651f2d + 138369c commit 735cf3f

18 files changed

+146
-83
lines changed

WordPress/WordPressTest/ApproveCommentActionTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import XCTest
33

44
final class ApproveCommentActionTests: XCTestCase {
55
private class TestableApproveComment: ApproveComment {
6-
let service = MockNotificationActionsService(managedObjectContext: TestContextManager.sharedInstance().mainContext)
6+
let service: MockNotificationActionsService
7+
78
override var actionsService: NotificationActionsService? {
89
return service
910
}
11+
12+
init(on: Bool, coreDataStack: CoreDataStack) {
13+
service = MockNotificationActionsService(managedObjectContext: coreDataStack.mainContext)
14+
super.init(on: on)
15+
}
1016
}
1117

1218
private class MockNotificationActionsService: NotificationActionsService {
@@ -26,6 +32,7 @@ final class ApproveCommentActionTests: XCTestCase {
2632

2733
private var action: ApproveComment?
2834
private let utility = NotificationUtility()
35+
private var contextManager: TestContextManager!
2936

3037
private struct Constants {
3138
static let initialStatus: Bool = false
@@ -34,7 +41,8 @@ final class ApproveCommentActionTests: XCTestCase {
3441
override func setUp() {
3542
super.setUp()
3643
utility.setUp()
37-
action = TestableApproveComment(on: Constants.initialStatus)
44+
contextManager = TestContextManager()
45+
action = TestableApproveComment(on: Constants.initialStatus, coreDataStack: contextManager)
3846
makeNetworkAvailable()
3947
}
4048

WordPress/WordPressTest/Classes/Stores/ActivityStoreTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ class ActivityStoreTests: XCTestCase {
99
private var store: ActivityStore!
1010
private var activityServiceMock: ActivityServiceRemoteMock!
1111
private var backupServiceMock: JetpackBackupServiceMock!
12+
private var contextManager: TestContextManager!
1213

1314
override func setUp() {
1415
super.setUp()
1516

17+
contextManager = TestContextManager()
1618
dispatcher = ActionDispatcher()
1719
activityServiceMock = ActivityServiceRemoteMock()
18-
backupServiceMock = JetpackBackupServiceMock()
20+
backupServiceMock = JetpackBackupServiceMock(managedObjectContext: contextManager.mainContext)
1921
store = ActivityStore(dispatcher: dispatcher, activityServiceRemote: activityServiceMock, backupService: backupServiceMock)
2022
}
2123

2224
override func tearDown() {
25+
ContextManager.overrideSharedInstance(nil)
2326
dispatcher = nil
2427
store = nil
2528

@@ -248,10 +251,6 @@ extension ActivityGroup {
248251
class JetpackBackupServiceMock: JetpackBackupService {
249252
var didCallGetAllBackupStatusWithSite: JetpackSiteRef?
250253

251-
init() {
252-
super.init(managedObjectContext: TestContextManager.sharedInstance().mainContext)
253-
}
254-
255254
override func getAllBackupStatus(for site: JetpackSiteRef, success: @escaping ([JetpackBackup]) -> Void, failure: @escaping (Error) -> Void) {
256255
didCallGetAllBackupStatusWithSite = site
257256

WordPress/WordPressTest/ContextManagerMock.m

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ @implementation ContextManagerMock
1414
@synthesize mainContext = _mainContext;
1515
@synthesize managedObjectModel = _managedObjectModel;
1616

17-
- (instancetype)init
18-
{
19-
self = [super init];
20-
if (self) {
21-
// Override the shared ContextManager
22-
[ContextManager internalSharedInstance];
23-
[ContextManager overrideSharedInstance:self];
24-
}
25-
26-
return self;
27-
}
28-
2917
- (NSManagedObjectModel *)managedObjectModel
3018
{
3119
return _managedObjectModel ?: [super managedObjectModel];

WordPress/WordPressTest/EditCommentActionTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import XCTest
33

44
final class EditCommentActionTests: XCTestCase {
55
private class TestableEditComment: EditComment {
6-
let service = MockNotificationActionsService(managedObjectContext: TestContextManager.sharedInstance().mainContext)
6+
let service: MockNotificationActionsService
7+
78
override var actionsService: NotificationActionsService? {
89
return service
910
}
11+
12+
init(on: Bool, coreDataStack: CoreDataStack) {
13+
service = MockNotificationActionsService(managedObjectContext: coreDataStack.mainContext)
14+
super.init(on: on)
15+
}
1016
}
1117

1218
private class MockNotificationActionsService: NotificationActionsService {
@@ -20,6 +26,7 @@ final class EditCommentActionTests: XCTestCase {
2026

2127
private var action: EditComment?
2228
private let utility = NotificationUtility()
29+
private var contextManager: TestContextManager!
2330

2431
private struct Constants {
2532
static let initialStatus: Bool = false
@@ -28,12 +35,14 @@ final class EditCommentActionTests: XCTestCase {
2835
override func setUp() {
2936
super.setUp()
3037
utility.setUp()
31-
action = TestableEditComment(on: Constants.initialStatus)
38+
contextManager = TestContextManager()
39+
action = TestableEditComment(on: Constants.initialStatus, coreDataStack: contextManager)
3240
}
3341

3442
override func tearDown() {
3543
action = nil
3644
utility.tearDown()
45+
ContextManager.overrideSharedInstance(nil)
3746
super.tearDown()
3847
}
3948

WordPress/WordPressTest/LikeCommentActionTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import XCTest
33

44
final class LikeCommentActionTests: XCTestCase {
55
private class TestableLikeComment: LikeComment {
6-
let service = MockNotificationActionsService(managedObjectContext: TestContextManager.sharedInstance().mainContext)
6+
let service: MockNotificationActionsService
7+
78
override var actionsService: NotificationActionsService? {
89
return service
910
}
11+
12+
init(on: Bool, coreDataStack: CoreDataStack) {
13+
service = MockNotificationActionsService(managedObjectContext: coreDataStack.mainContext)
14+
super.init(on: on)
15+
}
1016
}
1117

1218
private class MockNotificationActionsService: NotificationActionsService {
@@ -26,6 +32,7 @@ final class LikeCommentActionTests: XCTestCase {
2632

2733
private var action: LikeComment?
2834
private let utility = NotificationUtility()
35+
private var contextManager: TestContextManager!
2936

3037
private struct Constants {
3138
static let initialStatus: Bool = false
@@ -34,7 +41,8 @@ final class LikeCommentActionTests: XCTestCase {
3441
override func setUp() {
3542
super.setUp()
3643
utility.setUp()
37-
action = TestableLikeComment(on: Constants.initialStatus)
44+
contextManager = TestContextManager()
45+
action = TestableLikeComment(on: Constants.initialStatus, coreDataStack: contextManager)
3846
makeNetworkAvailable()
3947
}
4048

WordPress/WordPressTest/MarkAsSpamActionTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import XCTest
33

44
final class MarkAsSpamActionTests: XCTestCase {
55
private class TestableMarkAsSpam: MarkAsSpam {
6-
let service = MockNotificationActionsService(managedObjectContext: TestContextManager.sharedInstance().mainContext)
6+
let service: MockNotificationActionsService
7+
78
override var actionsService: NotificationActionsService? {
89
return service
910
}
11+
12+
init(on: Bool, coreDataStack: CoreDataStack) {
13+
service = MockNotificationActionsService(managedObjectContext: coreDataStack.mainContext)
14+
super.init(on: on)
15+
}
1016
}
1117

1218
private class MockNotificationActionsService: NotificationActionsService {
@@ -17,6 +23,7 @@ final class MarkAsSpamActionTests: XCTestCase {
1723

1824
private var action: MarkAsSpam?
1925
let utility = NotificationUtility()
26+
private var testContextManager: TestContextManager!
2027

2128
private struct Constants {
2229
static let initialStatus: Bool = false
@@ -25,7 +32,8 @@ final class MarkAsSpamActionTests: XCTestCase {
2532
override func setUp() {
2633
super.setUp()
2734
utility.setUp()
28-
action = TestableMarkAsSpam(on: Constants.initialStatus)
35+
testContextManager = TestContextManager()
36+
action = TestableMarkAsSpam(on: Constants.initialStatus, coreDataStack: testContextManager)
2937
makeNetworkAvailable()
3038
}
3139

WordPress/WordPressTest/Menus/Controllers/MenuItemsViewControllerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MenuItemsViewControllerTests: XCTestCase {
1111

1212
override func tearDownWithError() throws {
1313
context = nil
14-
TestContextManager.overrideSharedInstance(nil)
14+
ContextManager.overrideSharedInstance(nil)
1515
try super.tearDownWithError()
1616
}
1717

WordPress/WordPressTest/Menus/MenuItemTests.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import Foundation
33

44
class MenuItemTests: XCTestCase {
55

6-
private var context: NSManagedObjectContext!
6+
private var contextManager: TestContextManager!
7+
private var context: NSManagedObjectContext! {
8+
contextManager.mainContext
9+
}
710

8-
override func setUpWithError() throws {
9-
context = TestContextManager().mainContext
11+
override func setUp() {
12+
contextManager = TestContextManager()
1013
}
1114

12-
override func tearDownWithError() throws {
13-
TestContextManager.overrideSharedInstance(nil)
15+
override func tearDown() {
16+
ContextManager.overrideSharedInstance(nil)
1417
context.reset()
15-
context = nil
1618
}
1719

1820
/// Tests detection of descendants.

WordPress/WordPressTest/Pages/Controllers/PageListViewControllerTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import Nimble
66

77
class PageListViewControllerTests: XCTestCase {
88

9-
private var context: NSManagedObjectContext!
9+
private var contextManager: TestContextManager!
10+
private var context: NSManagedObjectContext! {
11+
contextManager.mainContext
12+
}
1013

1114
override func setUp() {
12-
context = TestContextManager().mainContext
15+
contextManager = TestContextManager()
1316
super.setUp()
1417
}
1518

1619
override func tearDown() {
17-
context = nil
18-
TestContextManager.overrideSharedInstance(nil)
20+
ContextManager.overrideSharedInstance(nil)
1921
super.tearDown()
2022
}
2123

WordPress/WordPressTest/ReaderPostTest.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44
@import WordPressShared;
55

66
@interface ReaderPostTest : XCTestCase
7+
8+
@property (nonatomic, strong) id<CoreDataStack> coreDataStack;
9+
710
@end
811

912
@implementation ReaderPostTest
1013

14+
@synthesize coreDataStack = coreDataStack;
15+
16+
- (void)setUp
17+
{
18+
self.coreDataStack = [[TestContextManager alloc] init];
19+
}
20+
21+
- (void)tearDown
22+
{
23+
self.coreDataStack = nil;
24+
}
25+
1126
- (void)testSiteIconForDisplay
1227
{
13-
NSManagedObjectContext *context = [[TestContextManager sharedInstance] mainContext];
28+
NSManagedObjectContext *context = [self.coreDataStack mainContext];
1429
ReaderPost *post = [NSEntityDescription insertNewObjectForEntityForName:@"ReaderPost"
1530
inManagedObjectContext:context];
1631

@@ -33,7 +48,7 @@ - (void)testSiteIconForDisplay
3348

3449
- (void)testDisplayDate
3550
{
36-
NSManagedObjectContext *context = [[TestContextManager sharedInstance] mainContext];
51+
NSManagedObjectContext *context = [self.coreDataStack mainContext];
3752
ReaderPost *post = [NSEntityDescription insertNewObjectForEntityForName:@"ReaderPost"
3853
inManagedObjectContext:context];
3954

0 commit comments

Comments
 (0)