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

Code before:
                var receivedObjects: [Issue]?
                _ = observable.map([Issue].self, using: decoder).subscribe(onNext: { objects in
                    receivedObjects = objects
                })
                expect(receivedObjects).notTo(beNil())
                expect(receivedObjects?.count) == 3
                expect(receivedObjects?.map { $0.title }) == ["Hello, Moya!", "Hello, Moya!", "Hello, Moya!"]
            }

            it("maps data representing a json at a key path to a decodable object") {
                let json: [String: Any] = ["issue": json] // nested json
                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 receivedObject: Issue?
                _ = observable.map(Issue.self, atKeyPath: "issue", using: decoder).subscribe(onNext: { object in
                    receivedObject = object
                })
                expect(receivedObject).notTo(beNil())
                expect(receivedObject?.title) == "Hello, Moya!"
                expect(receivedObject?.createdAt) == formatter.date(from: "1995-01-14T12:34:56")!
            }

            it("maps data representing a json array at a key path to a decodable object") {
                let json: [String: Any] = ["issues": [json]] // nested json array
                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 receivedObjects: [Issue]?
                _ = observable.map([Issue].self, atKeyPath: "issues", using: decoder).subscribe(onNext: { object in
                    receivedObjects = object
                })
                expect(receivedObjects).notTo(beNil())
                expect(receivedObjects?.count) == 1
                expect(receivedObjects?.first?.title) == "Hello, Moya!"
                expect(receivedObjects?.first?.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?

Review comment (bug):
nit: it would be nice to mark tests we add from bug reports with the bug report that found it. So adding something like bug from Moya/Moya#1311 would be awesome

Code after:
                var receivedObjects: [Issue]?
                _ = observable.map([Issue].self, using: decoder).subscribe(onNext: { objects in
                    receivedObjects = objects
                })
                expect(receivedObjects).notTo(beNil())
                expect(receivedObjects?.count) == 3
                expect(receivedObjects?.map { $0.title }) == ["Hello, Moya!", "Hello, Moya!", "Hello, Moya!"]
            }

            it("maps data representing a json at a key path to a decodable object") {
                let json: [String: Any] = ["issue": json] // nested json
                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 receivedObject: Issue?
                _ = observable.map(Issue.self, atKeyPath: "issue", using: decoder).subscribe(onNext: { object in
                    receivedObject = object
                })
                expect(receivedObject).notTo(beNil())
                expect(receivedObject?.title) == "Hello, Moya!"
                expect(receivedObject?.createdAt) == formatter.date(from: "1995-01-14T12:34:56")!
            }

            it("maps data representing a json array at a key path to a decodable object (#1311)") {
                let json: [String: Any] = ["issues": [json]] // nested json array
                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 receivedObjects: [Issue]?
                _ = observable.map([Issue].self, atKeyPath: "issues", using: decoder).subscribe(onNext: { object in
                    receivedObjects = object
                })
                expect(receivedObjects).notTo(beNil())
                expect(receivedObjects?.count) == 1
                expect(receivedObjects?.first?.title) == "Hello, Moya!"
                expect(receivedObjects?.first?.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?