Skip to content

Commit fe0831f

Browse files
committed
Fix tests from #61
Remove shared state from unit tests. Avoid multiple simultaneous observers and changes.
1 parent 95d3b3e commit fe0831f

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Tests/ObservationTests.swift

+16-14
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
// Copyright © 2021-present Jesse Squires
1212
//
1313

14-
@testable import Foil
1514
import Combine
15+
@testable import Foil
1616
import XCTest
1717

1818
let timeout = TimeInterval(5)
1919

2020
final class ObservationTests: XCTestCase {
2121

22-
let settings = TestSettings()
23-
2422
var cancellable = Set<AnyCancellable>()
2523

2624
var observer: NSKeyValueObservation?
@@ -31,11 +29,12 @@ final class ObservationTests: XCTestCase {
3129
}
3230

3331
func test_Integration_ProjectedValue() {
32+
let settings = TestSettings()
3433
let expectation = self.expectation(description: #function)
3534
let expectedValue = 1_000.0
3635
var publishedValue: Double?
3736

38-
self.settings.$average
37+
settings.$average
3938
.sink { newValue in
4039
publishedValue = newValue
4140

@@ -48,18 +47,19 @@ final class ObservationTests: XCTestCase {
4847
}
4948
.store(in: &self.cancellable)
5049

51-
self.settings.average = expectedValue
50+
settings.average = expectedValue
5251
self.wait(for: [expectation], timeout: timeout)
5352

54-
XCTAssertEqual(self.settings.average, publishedValue)
53+
XCTAssertEqual(settings.average, publishedValue)
5554
}
5655

5756
func test_Integration_ProjectedValue_ExternalChange() {
57+
let settings = TestSettings()
5858
let expectation = self.expectation(description: #function)
5959
let expectedValue = 1_000.0
6060
var publishedValue: Double?
6161

62-
self.settings.$average
62+
settings.$average
6363
.sink { newValue in
6464
publishedValue = newValue
6565

@@ -69,31 +69,33 @@ final class ObservationTests: XCTestCase {
6969
}
7070
.store(in: &self.cancellable)
7171

72-
type(of: self.settings).store.set(expectedValue, forKey: "average")
72+
type(of: settings).store.set(expectedValue, forKey: "average")
7373
self.wait(for: [expectation], timeout: timeout)
7474

75-
XCTAssertEqual(self.settings.average, publishedValue)
75+
XCTAssertEqual(settings.average, publishedValue)
7676
}
7777

7878
func test_Integration_Publisher() {
79+
let settings = TestSettings()
7980
let expectation = self.expectation(description: #function)
8081
var publishedValue: String?
8182

82-
self.settings
83+
settings
8384
.publisher(for: \.userId, options: [.new])
8485
.sink { newValue in
8586
publishedValue = newValue
8687
expectation.fulfill()
8788
}
8889
.store(in: &self.cancellable)
8990

90-
self.settings.userId = "test_publisher"
91+
settings.userId = "test_publisher"
9192
self.wait(for: [expectation], timeout: timeout)
9293

93-
XCTAssertEqual(self.settings.userId, publishedValue)
94+
XCTAssertEqual(settings.userId, publishedValue)
9495
}
9596

9697
func test_Integration_KVO() {
98+
let settings = TestSettings()
9799
let expectation = self.expectation(description: #function)
98100
var changedValue: String?
99101

@@ -105,9 +107,9 @@ final class ObservationTests: XCTestCase {
105107
expectation.fulfill()
106108
}
107109

108-
self.settings.userId = "test_kvo"
110+
settings.userId = "test_kvo"
109111
self.wait(for: [expectation], timeout: timeout)
110112

111-
XCTAssertEqual(self.settings.userId, changedValue)
113+
XCTAssertEqual(settings.userId, changedValue)
112114
}
113115
}

0 commit comments

Comments
 (0)