Created
November 9, 2024 17:48
-
-
Save robertmryan/889c68fa5fdec028b323e337e8a74ac5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class MyAppTests: XCTestCase { | |
var cancellables: Set<AnyCancellable> = [] | |
func testDemoDataIncorrect() { | |
let start = Date.now | |
// fire an event in 1 second, but only get the first value | |
let publisher = Timer.publish(every: 1, on: .main, in: .default) | |
.autoconnect() | |
.first() | |
publisher.sink { result in | |
if case .failure(let error) = result { | |
XCTAssert(true, "\(error)") | |
} | |
} receiveValue: { date in | |
XCTAssertLessThan(date, start) // should be XCTAssertGreaterThan(response, start) | |
} | |
.store(in: &cancellables) | |
} | |
func testDemoData() { | |
let start = Date.now | |
let expectation = expectation(description: "timer should fire") | |
let publisher = Timer.publish(every: 1, on: .main, in: .default) | |
.autoconnect() | |
.first() | |
.eraseToAnyPublisher() | |
publisher.sink { result in | |
if case .failure(let error) = result { | |
XCTAssert(true, "\(error)") | |
} | |
expectation.fulfill() | |
} receiveValue: { date in | |
XCTAssertGreaterThan(date, start) | |
} | |
.store(in: &cancellables) | |
waitForExpectations(timeout: 4) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few changes from the original example:
Rather than
Just
, I use aTimer.TimerPublisher
to consistently manifest the scenario where a publisher publishes a value after you have already returned fromtestDemoDataIncorrect
.An expectation can be used to make sure you don't return from
testDemoData
before the value is published.