{"id":17648827,"url":"https://github.com/wvteijlingen/moya-integration-testing","last_synced_at":"2025-05-07T06:04:26.844Z","repository":{"id":145263589,"uuid":"275207935","full_name":"wvteijlingen/moya-integration-testing","owner":"wvteijlingen","description":"A Moya plugin for easy integration testing of your Moya network requests using XCTest","archived":false,"fork":false,"pushed_at":"2020-07-07T18:34:04.000Z","size":26,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-07T06:04:19.967Z","etag":null,"topics":["alamofire","integration-testing","moya","networking","swift","testing","xctest"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wvteijlingen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2020-06-26T17:09:33.000Z","updated_at":"2020-07-14T18:55:12.000Z","dependencies_parsed_at":"2023-04-10T17:48:35.509Z","dependency_job_id":null,"html_url":"https://github.com/wvteijlingen/moya-integration-testing","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/wvteijlingen%2Fmoya-integration-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wvteijlingen%2Fmoya-integration-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wvteijlingen%2Fmoya-integration-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wvteijlingen%2Fmoya-integration-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wvteijlingen","download_url":"https://codeload.github.com/wvteijlingen/moya-integration-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252823920,"owners_count":21809713,"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":["alamofire","integration-testing","moya","networking","swift","testing","xctest"],"created_at":"2024-10-23T11:21:02.341Z","updated_at":"2025-05-07T06:04:26.829Z","avatar_url":"https://github.com/wvteijlingen.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://codecov.io/gh/wvteijlingen/moya-integration-testing\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/wvteijlingen/moya-integration-testing/branch/master/graph/badge.svg\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e\n  • \u003ca href=\"#setup\"\u003eSetup\u003c/a\u003e\n  • \u003ca href=\"#convenience-assertions\"\u003eConvenience assertions\u003c/a\u003e\n\u003c/p\u003e\n\n# moya-integration-testing\n\nA Moya plugin for easy integration testing of your Moya network requests.\n\n## Usage\n\nFor each one of your test cases you can configure which endpoints you expect your code to request. Per endpoint you can specify\nthe returned HTTP status code and body.\n\nAfter executing the code you want to test, use the assertion methods on the stubbed endpoints to assert that all they were requested,\nand to inspect the actual requests made to them.\n\n```swift\nfunc testFetchPosts() {\n    // Configure stubs for each endpoint that your code should request.\n    // Here we expect our code to make a GET request to \"/posts\", with a query parameter named \"search\".\n    // When that endpoint is requested, we respond with a 200 status code and an empty JSON array.\n    let postsEndpoint = tester.stub(\"https://example.com/posts?search=foo\", method: \"GET\", statusCode: 200, body: #\"[]\"#)\n\n    // Execute the code under test.\n    testSubject.fetchPosts(withSearch: \"foo\")\n\n    // Assert that the endpoint was requested exactly once by your code.\n    // The assertWasRequested method will fail the test if the endpoint was not requested.\n    postsEndpoint.assertWasRequested(with: { request in\n        // Optionally we can use a callback to assert that the request was as expected.\n        AssertHeaderEqual(request, \"Authorization\", \"Bearer secret-token\")\n    })\n\n    // You can also assert that an endpoint was requested multiple times, or not at all:\n    stubbedEndpoint.assertWasRequested(count: 2)\n    stubbedEndpoint.assertWasNotRequested()\n}\n```\n\n## Setup\n\n1. Use an instance of `MoyaIntegrationTester` as the last plugin in your `MoyaProvider`.\n2. Pass the `endpointClosure` function from the same `MoyaIntegrationTester` instance to the Moya provider.\n3. Enable stubbing on the Moya provider.\n\n```swift\nimport MoyaIntegrationTesting\n\nlet tester = MoyaIntegrationTester()\nlet provider = MoyaProvider(\n   endpointClosure: tester.endpointClosure(),\n   stubClosure: MoyaProvider.immediatelyStub,\n   plugins: [tester]\n)\n```\n\nIf your original Moya setup already uses a custom `EndpointClosure`,\nyou need to use `tester.endpointClosure(wrapping: yourOriginalClosure)` so that your custom closure is still called.\n\n## Convenience assertions\n\nThe package ships with a number of custom assertions that make it easier to check `URLRequest` instances:\n\n```swift\n/// Asserts that the given `request` contains an HTTP header with the given `key` and `value`.\n/// - Parameters:\n///   - request: The request to inspect.\n///   - key: The key of the expected HTTP header.\n///   - value: The expected value of the HTTP header.\n///   - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.\n///   - line: The line number on which failure occurred. Defaults to the line number on which this function was called.\npublic func AssertHeaderEqual(_ request: URLRequest, key: String, value: String)\n```\n\nAsserts that the given request contains an HTTP header with the given key and value.\n\n```swift\n/// Asserts that the body of the given `request` is equal to the expected body.\n/// - Parameters:\n///   - request: The request to inspect.\n///   - body: The expected HTTP body value.\n///   - encoding: The encoding of the body value.\n///   - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.\n///   - line: The line number on which failure occurred. Defaults to the line number on which this function was called.\npublic func AssertBodyEqual(_ request: URLRequest, _ body: String, encoding: String.Encoding = .utf8)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwvteijlingen%2Fmoya-integration-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwvteijlingen%2Fmoya-integration-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwvteijlingen%2Fmoya-integration-testing/lists"}