{"id":13339511,"url":"https://github.com/dtroupe18/NetworkTesting","last_synced_at":"2025-03-11T14:31:31.984Z","repository":{"id":123631990,"uuid":"133992736","full_name":"dtroupe18/NetworkTesting","owner":"dtroupe18","description":"Examples on how to test API calls","archived":false,"fork":false,"pushed_at":"2018-05-22T22:46:08.000Z","size":202,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T20:32:42.521Z","etag":null,"topics":["ios11","mockingjay","nimble","quick","spacex-api","swift4","unit-testing"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtroupe18.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-18T18:52:28.000Z","updated_at":"2018-05-22T22:46:09.000Z","dependencies_parsed_at":"2023-04-02T10:59:09.852Z","dependency_job_id":null,"html_url":"https://github.com/dtroupe18/NetworkTesting","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtroupe18%2FNetworkTesting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtroupe18%2FNetworkTesting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtroupe18%2FNetworkTesting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtroupe18%2FNetworkTesting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtroupe18","download_url":"https://codeload.github.com/dtroupe18/NetworkTesting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243051861,"owners_count":20228282,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios11","mockingjay","nimble","quick","spacex-api","swift4","unit-testing"],"created_at":"2024-07-29T19:20:21.427Z","updated_at":"2025-03-11T14:31:31.931Z","avatar_url":"https://github.com/dtroupe18.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NetworkTesting\n\nExamples on how to unit test API calls using: \n\n[SpaceX API](https://github.com/r-spacex/SpaceX-API)\n\n[Mockingjay](https://github.com/kylef/Mockingjay)\n[Quick](https://github.com/Quick/Quick)\n[Nimble](https://github.com/Quick/Nimble)\n\n### Unit Test using Mocked Data\n\n```swift\noverride func spec() {\n    super.spec()\n    \n    describe(\"requestRocketWithNameWithoutApiCall\") {\n        context(\"sucess\") {\n            it(\"returns rocketData\") {\n                var returnedRocketData: Rocket?\n                \n                // Get the stubbed response from inside the Fixtures directory. This is response data from the\n                // SpaceX API that we know is correctly formatted. Thuse we have removed outside factors from impacting the\n                // result of this test.\n                \n                // When running tests the application bundle is still the main bundle. So we need to get the test bundle\n                let bundle: Bundle = Bundle(for: type(of: self))\n                if let path: String = bundle.path(forResource: \"GetRocketSuccess\", ofType: \"json\") {\n                    let url: URL = URL(fileURLWithPath: path)\n                    \n                    do {\n                        let simulatedJsonData: Data = try Data(contentsOf: url)\n                        \n                        // Stub the url so that all requests to this url will use stored JSON data\n                        self.stub(uri(\"https://api.spacexdata.com/v2/rockets/falconheavy\"), delay: nil, jsonData(simulatedJsonData))\n                        \n                        // Here make the request which will use our stubbed data\n                        ApiClient.getRocketInfo(rocketName: \"falconheavy\", onSuccess: { rocketData in\n                            returnedRocketData = rocketData\n                        })\n                        \n                        // Check that we got rocket data\n                        expect(returnedRocketData).toEventuallyNot(beNil())\n                        \n                        // Check that values were parsed correctly\n                        expect(returnedRocketData?.name) == \"Falcon Heavy\"\n                        expect(returnedRocketData?.engines.thrustVacuum.lbf) == 205500\n                        expect(returnedRocketData?.costPerLaunch) == 90000000\n                    } catch {\n                        fail(\"Error thrown: \\(error)\")\n                    }\n                } else {\n                    fail(\"Could not load GetRocketSuccess.json\")\n                }\n            }\n        }\n        \n        // Error Case\n        context(\"error\") {\n            it(\"returns error\") {\n                var returnedError: Error?\n                \n                let expectedError: Error = NSError(domain: \"Error here\", code: 404, userInfo: nil) as Error\n                self.stub(uri(\"https://api.spacexdata.com/v2/rockets/falconheavy\"), delay: nil, failure(expectedError as NSError))\n                \n                ApiClient.getRocketInfo(rocketName: \"falconheavy\", onError: { error in\n                    returnedError = error\n                })\n                expect(returnedError).toEventuallyNot(beNil())\n            }\n        }\n        \n        context(\"jsonStructureIncorrect\") {\n            it(\"returns error\") {\n                var returnedError: Error?\n                // Error we return in our ApiClient if codable fails\n                let expectedError: Error = NSError(domain: \"\", code: 0, userInfo: [NSLocalizedDescriptionKey : \"Error data incorrectly formatted\"]) as Error\n                \n                let bundle: Bundle = Bundle(for: type(of: self))\n                if let path: String = bundle.path(forResource: \"GetRocketFailure\", ofType: \"json\") {\n                    let url: URL = URL(fileURLWithPath: path)\n                    \n                    do {\n                        let simulatedJsonData: Data = try Data(contentsOf: url)\n                        \n                        // Stub the url so that all requests to this url will use the stored JSON data\n                        self.stub(uri(\"https://api.spacexdata.com/v2/rockets/falconheavy\"), delay: nil, jsonData(simulatedJsonData))\n                        \n                        ApiClient.getRocketInfo(rocketName: \"falconheavy\", onError: { error in\n                            returnedError = error\n                        })\n                        expect(returnedError).toEventuallyNot(beNil())\n                        // Make sure the correct error was returned\n                        expect(returnedError?.localizedDescription) == expectedError.localizedDescription\n                    } catch {\n                        fail(\"Error thrown: \\(error)\")\n                    }\n                } else {\n                    fail(\"Could not load GetRocketSuccess.json\")\n                }\n            }\n        }\n    }\n}\n```\n\n\n\n\n\n\n### Test making an actual API call (not recommended)\n```swift\n// Generally you don't make a test like this because it depends on outside factors in order to be successful.\n// This is just an example of how it can be done.\n    \n    override func spec() {\n        describe(\"requestRocketWithName\") {\n            context(\"sucess\") {\n                it(\"returns rocketData\") {\n                    var returnedRocketData: Rocket?\n                    \n                    // Here we are making an actual network request and checking that the returned data is valid\n                    ApiClient.getRocketInfo(rocketName: \"falconheavy\", onSuccess: { rocketData in\n                        returnedRocketData = rocketData\n                    })\n                    \n                    // Give the request 15 seconds to finish\n                    expect(returnedRocketData).toEventuallyNot(beNil(), timeout: 15)\n                }\n            }\n        }\n    }\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtroupe18%2FNetworkTesting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtroupe18%2FNetworkTesting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtroupe18%2FNetworkTesting/lists"}