Created
April 29, 2022 08:29
-
-
Save tigi44/36461df3333885ec185bbd6857a572c1 to your computer and use it in GitHub Desktop.
Unit testing Combine/Result Swift code
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
import XCTest | |
import Combine | |
public extension XCTestCase { | |
func awaitPublisher<T: Publisher>( | |
_ publisher: T, | |
timeout: TimeInterval = 10, | |
file: StaticString = #file, | |
line: UInt = #line | |
) throws -> T.Output { | |
var result: Result<T.Output, Error>? | |
let expectation = self.expectation(description: "Awaiting publisher") | |
let cancellable = publisher.sink( | |
receiveCompletion: { completion in | |
switch completion { | |
case .failure(let error): | |
result = .failure(error) | |
case .finished: | |
break | |
} | |
expectation.fulfill() | |
}, | |
receiveValue: { value in | |
result = .success(value) | |
} | |
) | |
waitForExpectations(timeout: timeout) | |
cancellable.cancel() | |
let unwrappedResult = try XCTUnwrap( | |
result, | |
"Awaited publisher did not produce any output", | |
file: file, | |
line: line | |
) | |
return try unwrappedResult.get() | |
} | |
} |
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
import XCTest | |
import Combine | |
public extension XCTestCase { | |
func XCTAssertResult<T, E>( | |
_ result: Result<T, E>, | |
isSuccessWith value: T, | |
message: (E) -> String = { "Expected to be a success but got a failure with \($0) "}, | |
file: StaticString = #filePath, | |
line: UInt = #line | |
) where E: Error, T: Equatable { | |
switch result { | |
case .failure(let error): | |
XCTFail(message(error), file: file, line: line) | |
case .success(let resultValue): | |
XCTAssertEqual(resultValue, value) | |
} | |
} | |
func XCTAssertResult<T, E>( | |
_ result: Result<T, E>, | |
isFailureWith error: E, | |
message: (T) -> String = { "Expected to be a failure but got a success with \($0) "}, | |
file: StaticString = #filePath, | |
line: UInt = #line | |
) where E: Equatable & Error { | |
switch result { | |
case .failure(let resultError): | |
XCTAssertEqual(resultError, error) | |
case .success(let value): | |
XCTFail(message(value), file: file, line: line) | |
} | |
} | |
func XCTAssertResult<T, E>( | |
_ result: Result<T, E>, | |
isSuccessWith assertClosure: (T) -> Void, | |
message: (E) -> String = { "Expected to be a success but got a failure with \($0) "}, | |
file: StaticString = #filePath, | |
line: UInt = #line | |
) where E: Error { | |
switch result { | |
case .failure(let error): | |
XCTFail(message(error), file: file, line: line) | |
case .success(let resultValue): | |
assertClosure(resultValue) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unit testing Combine-based Swift code