mokagio / CombineTestHelpers

Custom XCTest assertions for Combine types

Home Page:https://mokacoding.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CombineTestHelpers

Custom XCTest assertions that turn this:

let expectation = XCTestExpectation(description: "Publishes one value then finishes")

var cancellables = Set<AnyCancellable>()
var values = [Int]()

publisher
    .sink(
        receiveCompletion: { completion in
            guard case .finished = completion else { return }
            expectation.fulfill()
        },
        receiveValue: { value in
            guard values.isEmpty else {
                return XCTFail("Expected to receive only one value, got another: (\(value))")
            }
            XCTAssertEqual(value, 42)
            values.append(value)
        }
    )
    .store(in: &cancellables)

wait(for: [expectation], timeout: 0.5)

into this:

assert(publisher, eventuallyFinishesPublishingOnly: 42)

To learn more about testing in Swift, checkout my book Test-Driven Development in Swift with SwiftUI and Combine.

About

Custom XCTest assertions for Combine types

https://mokacoding.com


Languages

Language:Swift 96.3%Language:Ruby 2.5%Language:Shell 1.2%