Skip to content

Instantly share code, notes, and snippets.

@Andrea-Scuderi
Created September 3, 2019 12:37
Show Gist options
  • Save Andrea-Scuderi/985f73a2fa2fa99576fbc8882150af30 to your computer and use it in GitHub Desktop.
Save Andrea-Scuderi/985f73a2fa2fa99576fbc8882150af30 to your computer and use it in GitHub Desktop.
XCTTest Publisher utils
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