Issue #506
When a function expects AnyPublisher<[Book], Error> but in mock, we have Just
func getBooks() -> AnyPublisher<[Book], Error> {
    return Just([
        Book(id: "1", name: "Book 1"),
        Book(id: "2", name: "Book 2"),
    ])
    .eraseToAnyPublisher()
}
There will be a mismatch, hence compile error
Cannot convert return expression of type ‘AnyPublisher<[Book], Just
The reason is because Just produces Never, not Error. The workaround is to introduce Error
enum AppError: Error {
    case impossible
}
func getBooks() -> AnyPublisher<[Book], Error> {
    return Just([
        Book(id: "1", name: "Book 1"),
        Book(id: "2", name: "Book 2"),
    ])
    .mapError({ _ in  AppError.impossible })
    .eraseToAnyPublisher()
}
Start the conversation