Код: Выделить всё
class Example: ObservableObject {
@Published var isError = false
var cancellable: AnyCancellable?
private let failure: Bool
// MARK: - Initialization
init(failure: Bool) {
self.failure = failure
}
// MARK: - Internal
func fetch() {
isError = false
cancellable = fetch(type: "abc")
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { [weak self, type] result in
if case .failure = result {
self?.isError = true
}
}, receiveValue: { [weak self] value in
print(value)
})
.eraseToAnyPublisher()
}
// MARK: - Private
private func fetch(type: String) -> Future {
Future { [weak self] promise in
Task {
do {
let value = try await self?.check(type: type)
promise(.success(value))
} catch {
promise(.failure(error))
}
}
}
}
private func check(type: String) async throws -> String {
// do some acync fetch
if failure {
throw MyError.example
} else {
return "result: \(type)"
}
}
}
enum MyError: Error {
case example
}
Код: Выделить всё
func testWithSuccess() {
let sut = Example(failure: false)
XCTAssertFalse(sut.isError)
sut.fetch()
XCTAssertTrue(sut.isError)
}
Это упрощенный пример, указывающий на проблему.
Подробнее здесь: https://stackoverflow.com/questions/798 ... unit-tests
Мобильная версия