Created
September 3, 2019 12:37
-
-
Save Andrea-Scuderi/985f73a2fa2fa99576fbc8882150af30 to your computer and use it in GitHub Desktop.
XCTTest Publisher utils
This file contains 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
func evalValidResponseTest<T:Publisher>(publisher: T?) -> (expectations:[XCTestExpectation], cancellable: AnyCancellable?) { | |
XCTAssertNotNil(publisher) | |
let expectationFinished = expectation(description: "finished") | |
let expectationReceive = expectation(description: "receiveValue") | |
let expectationFailure = expectation(description: "failure") | |
expectationFailure.isInverted = true | |
let cancellable = publisher?.sink (receiveCompletion: { (completion) in | |
switch completion { | |
case .failure(let error): | |
print("--TEST ERROR--") | |
print(error.localizedDescription) | |
print("------") | |
expectationFailure.fulfill() | |
case .finished: | |
expectationFinished.fulfill() | |
} | |
}, receiveValue: { response in | |
XCTAssertNotNil(response) | |
print(response) | |
expectationReceive.fulfill() | |
}) | |
return (expectations: [expectationFinished, expectationReceive, expectationFailure], | |
cancellable: cancellable) | |
} | |
func evalInvalidResponseTest<T:Publisher>(publisher: T?) -> (expectations:[XCTestExpectation], cancellable: AnyCancellable?) { | |
XCTAssertNotNil(publisher) | |
let expectationFinished = expectation(description: "Invalid.finished") | |
expectationFinished.isInverted = true | |
let expectationReceive = expectation(description: "Invalid.receiveValue") | |
expectationReceive.isInverted = true | |
let expectationFailure = expectation(description: "Invalid.failure") | |
let cancellable = publisher?.sink (receiveCompletion: { (completion) in | |
switch completion { | |
case .failure(let error): | |
print("--TEST FULFILLED--") | |
print(error.localizedDescription) | |
print("------") | |
expectationFailure.fulfill() | |
case .finished: | |
expectationFinished.fulfill() | |
} | |
}, receiveValue: { response in | |
XCTAssertNotNil(response) | |
print(response) | |
expectationReceive.fulfill() | |
}) | |
return (expectations: [expectationFinished, expectationReceive, expectationFailure], | |
cancellable: cancellable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment