Skip to content

Commit 1f5c972

Browse files
committed
Remove shared instance from TestContextManager
1 parent 59f951a commit 1f5c972

17 files changed

+141
-69
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/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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ 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+
super.setUp()
13+
contextManager = TestContextManager()
1014
}
1115

12-
override func tearDownWithError() throws {
13-
TestContextManager.overrideSharedInstance(nil)
16+
override func tearDown() {
17+
super.tearDown()
18+
ContextManager.overrideSharedInstance(nil)
1419
context.reset()
15-
context = nil
1620
}
1721

1822
/// 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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@
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+
[super setUp];
19+
self.coreDataStack = [[TestContextManager alloc] init];
20+
}
21+
22+
- (void)tearDown
23+
{
24+
[super tearDown];
25+
self.coreDataStack = nil;
26+
}
27+
1128
- (void)testSiteIconForDisplay
1229
{
13-
NSManagedObjectContext *context = [[TestContextManager sharedInstance] mainContext];
30+
NSManagedObjectContext *context = [self.coreDataStack mainContext];
1431
ReaderPost *post = [NSEntityDescription insertNewObjectForEntityForName:@"ReaderPost"
1532
inManagedObjectContext:context];
1633

@@ -33,7 +50,7 @@ - (void)testSiteIconForDisplay
3350

3451
- (void)testDisplayDate
3552
{
36-
NSManagedObjectContext *context = [[TestContextManager sharedInstance] mainContext];
53+
NSManagedObjectContext *context = [self.coreDataStack mainContext];
3754
ReaderPost *post = [NSEntityDescription insertNewObjectForEntityForName:@"ReaderPost"
3855
inManagedObjectContext:context];
3956

WordPress/WordPressTest/ReaderSelectInterestsCoordinatorTests.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import XCTest
22
@testable import WordPress
33

44
class ReaderSelectInterestsCoordinatorTests: XCTestCase {
5+
private var contextManager: TestContextManager!
6+
7+
override func setUp() {
8+
super.setUp()
9+
contextManager = TestContextManager()
10+
}
11+
12+
override func tearDown() {
13+
super.tearDown()
14+
ContextManager.overrideSharedInstance(nil)
15+
}
16+
517
func testisFollowingInterestsReturnsFalse() {
618
let store = EphemeralKeyValueDatabase()
7-
let service = MockFollowedInterestsService(populateItems: false)
19+
let service = MockFollowedInterestsService(populateItems: false, coreDataStack: contextManager)
820
let coordinator = ReaderSelectInterestsCoordinator(service: service, store: store, userId: 1)
921

1022
service.success = true
@@ -22,7 +34,7 @@ class ReaderSelectInterestsCoordinatorTests: XCTestCase {
2234

2335
func testisFollowingInterestsReturnsTrue() {
2436
let store = EphemeralKeyValueDatabase()
25-
let service = MockFollowedInterestsService(populateItems: true)
37+
let service = MockFollowedInterestsService(populateItems: true, coreDataStack: contextManager)
2638
let coordinator = ReaderSelectInterestsCoordinator(service: service, store: store, userId: 1)
2739

2840
let successExpectation = expectation(description: "Fetching of interests succeeds")
@@ -42,7 +54,7 @@ class ReaderSelectInterestsCoordinatorTests: XCTestCase {
4254

4355
func testSaveInterestsTriggersSuccess() {
4456
let store = EphemeralKeyValueDatabase()
45-
let service = MockFollowedInterestsService(populateItems: false)
57+
let service = MockFollowedInterestsService(populateItems: false, coreDataStack: contextManager)
4658
let coordinator = ReaderSelectInterestsCoordinator(service: service, store: store, userId: nil)
4759

4860
let successExpectation = expectation(description: "Saving of interests callback returns true")
@@ -58,7 +70,7 @@ class ReaderSelectInterestsCoordinatorTests: XCTestCase {
5870

5971
func testSaveInterestsTriggersFailure() {
6072
let store = EphemeralKeyValueDatabase()
61-
let service = MockFollowedInterestsService(populateItems: false)
73+
let service = MockFollowedInterestsService(populateItems: false, coreDataStack: contextManager)
6274
let coordinator = ReaderSelectInterestsCoordinator(service: service, store: store, userId: nil)
6375

6476
service.success = false
@@ -86,14 +98,13 @@ class MockFollowedInterestsService: ReaderFollowedInterestsService {
8698

8799
private let failureError = NSError(domain: "org.wordpress.reader-tests", code: 1, userInfo: nil)
88100

89-
private var testContextManager: CoreDataStack?
90-
private var context: NSManagedObjectContext!
91-
92-
init(populateItems: Bool) {
93-
94-
testContextManager = TestContextManager.sharedInstance()
95-
context = testContextManager?.mainContext
101+
private var coreDataStack: CoreDataStack?
102+
private var context: NSManagedObjectContext! {
103+
coreDataStack?.mainContext
104+
}
96105

106+
init(populateItems: Bool, coreDataStack: CoreDataStack) {
107+
self.coreDataStack = coreDataStack
97108
self.populateItems = populateItems
98109
}
99110

0 commit comments

Comments
 (0)