Skip to content

Instantly share code, notes, and snippets.

@aduuub
Created November 1, 2023 22:55
Show Gist options
  • Save aduuub/a84b9994257d8dcfd8393baf25efc65a to your computer and use it in GitHub Desktop.
Save aduuub/a84b9994257d8dcfd8393baf25efc65a to your computer and use it in GitHub Desktop.
ResultPublishers Tests
import XCTest
// Depends on SP: https://github.com/albertbori/TestableCombinePublishers
import TestableCombinePublishers
final class ResultPublisherTests: XCTestCase {
func testSuccess() {
ResultPublisher<String, Error> { [weak self] in
self?.basicSuccess(completion: $0)
}
.expect("success string")
.waitForExpectations(timeout: 1)
}
func testFailure() {
ResultPublisher<String, Error> { [weak self] in
self?.basicFailure(completion: $0)
}
.expectFailure()
.waitForExpectations(timeout: 1)
}
func testMultiOutput() {
ResultPublisher<String, Error> { [weak self] in
self?.multiSuccess(completion: $0)
}
.expectExactly(2, { XCTAssertEqual("success string", $0) })
.waitForExpectations(timeout: 1)
}
// MARK: - HELPERS
private func basicFailure(completion: @escaping (Result<String, Error>) -> Void) {
completion(.failure(NSError(domain: "test", code: 1)))
}
private func basicSuccess(completion: @escaping (Result<String, Error>) -> Void) {
completion(.success("success string"))
}
private func multiSuccess(completion: @escaping (Result<String, Error>) -> Void) {
completion(.success("success string"))
completion(.success("success string"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment