Repository: Moya/Moya
File: Tests/Observable+MoyaSpec.swift (Swift)

Code before:
                expect(receivedObject?.title) == "Hello, Moya!"
                expect(receivedObject?.createdAt) == formatter.date(from: "1995-01-14T12:34:56")!
            }

            it("ignores invalid data") {
                var json = json
                json["createdAt"] = "Hahaha" // invalid date string
                guard let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) else {
                    preconditionFailure("Failed creating Data from JSON dictionary")
                }
                let observable = Response(statusCode: 200, data: data).asObservable()

                var receivedError: Error?
                _ = observable.map(Issue.self, using: decoder).subscribe { event in
                    switch event {
                    case .next:
                        fail("next called for invalid data")
                    case .error(let error):
                        receivedError = error
                    default:
                        break
                    }
                }

                // check whether we properly got a MoyaError
                expect(receivedError).to(Predicate<Error> { expression in
                    let receivedError = try expression.evaluate()
                    if case MoyaError.objectMapping? = receivedError {
                        return PredicateResult(status: .matches, message: .expectedActualValueTo("MoyaError.objectMapping"))
                    } else {
                        return PredicateResult(status: .doesNotMatch, message: .expectedActualValueTo("MoyaError.objectMapping"))
                    }
                })

                // check whether the MoyaError has a DecodingError as an associated value
                if case let MoyaError.objectMapping(decodingError, _)? = receivedError {
                    expect(decodingError).to(beAKindOf(DecodingError.self))
                } else {
                    preconditionFailure()
                }
            }
        }
    }
}


Review comment (suggestion):
hmm, isn't `MoyaError` equatable? if not, maybe we can make it so (at least for tests), would make this test cleaner

Code after:
                expect(receivedObject?.title) == "Hello, Moya!"
                expect(receivedObject?.createdAt) == formatter.date(from: "1995-01-14T12:34:56")!
            }

            it("ignores invalid data") {
                var json = json
                json["createdAt"] = "Hahaha" // invalid date string
                guard let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) else {
                    preconditionFailure("Failed creating Data from JSON dictionary")
                }
                let observable = Response(statusCode: 200, data: data).asObservable()

                var receivedError: Error?
                _ = observable.map(Issue.self, using: decoder).subscribe { event in
                    switch event {
                    case .next:
                        fail("next called for invalid data")
                    case .error(let error):
                        receivedError = error
                    default:
                        break
                    }
                }

                if case let MoyaError.objectMapping(nestedError, _)? = receivedError {
                    expect(nestedError).to(beAKindOf(DecodingError.self))
                } else {
                    fail("expected <MoyaError.objectMapping>, got <\(String(describing: receivedError))>")
                }
            }
        }
    }
}