{"id":32164323,"url":"https://github.com/stephanos/rewire","last_synced_at":"2025-10-21T14:44:45.251Z","repository":{"id":40751672,"uuid":"298662616","full_name":"stephanos/rewire","owner":"stephanos","description":"Dependency injection for Elixir. Zero code changes required.","archived":false,"fork":false,"pushed_at":"2024-07-07T01:17:09.000Z","size":75,"stargazers_count":110,"open_issues_count":3,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-09-13T07:38:04.437Z","etag":null,"topics":["dependency-injection","elixir","mocking"],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/rewire","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stephanos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-09-25T19:29:41.000Z","updated_at":"2025-08-10T14:11:18.000Z","dependencies_parsed_at":"2024-07-07T02:27:39.864Z","dependency_job_id":"854c1392-aa8b-4020-b055-497a4429af12","html_url":"https://github.com/stephanos/rewire","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/stephanos/rewire","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephanos%2Frewire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephanos%2Frewire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephanos%2Frewire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephanos%2Frewire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stephanos","download_url":"https://codeload.github.com/stephanos/rewire/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephanos%2Frewire/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279517035,"owners_count":26183896,"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","status":"online","status_checked_at":"2025-10-18T02:00:06.492Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dependency-injection","elixir","mocking"],"created_at":"2025-10-21T14:44:44.498Z","updated_at":"2025-10-21T14:44:45.243Z","avatar_url":"https://github.com/stephanos.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rewire\n\n[![Build Status](https://travis-ci.org/stephanos/rewire.svg?branch=master)](https://travis-ci.org/stephanos/rewire)\n[![Hex.pm](https://img.shields.io/hexpm/v/rewire.svg)](https://hex.pm/packages/rewire)\n\n`rewire` is a **dependency injection** library.\n\nIt keeps your application code completely free from testing concerns.\n\nAnd you can bring your own mock (`mox` is recommended).\n\n## Installation\n\nJust add `rewire` to your list of dependencies in mix.exs:\n\n```elixir\ndef deps do\n  [\n    {:rewire, \"~\u003e 0.10\", only: :test}\n  ]\nend\n```\n\n## Usage\n\nGiven a module such as this:\n\n```elixir\n# this module has a hard-wired dependency on the `English` module\ndefmodule Conversation do\n  @punctuation \"!\"\n  def start(), do: English.greet() \u003c\u003e @punctuation\nend\n```\n\nIf you define a `mox` mock `EnglishMock` you can rewire the dependency in your unit test:\n\n```elixir\ndefmodule MyTest do\n  use ExUnit.Case, async: true\n  import Rewire                                  # (1) activate `rewire`\n  import Mox\n\n  rewire Conversation, English: EnglishMock      # (2) rewire `English` to `EnglishMock`\n\n  test \"start/0\" do\n    stub(EnglishMock, :greet, fn -\u003e \"g'day\" end)\n    assert Conversation.start() == \"g'day!\"      # (3) test using the mock\n  end\nend\n```\n\nThis example uses `mox`, but `rewire` is mocking library-agnostic.\n\nYou can use multiple `rewire`s and multiple overrides:\n\n```elixir\n  rewire Conversation, English: EnglishMock\n  rewire OnlineConversation, Email: EmailMock, Chat: ChatMock\n```\n\nYou can also give the alias a different name using `as`:\n\n```elixir\n  rewire Conversation, English: EnglishMock, as: SmallTalk\n```\n\nNote that the `rewire` acts like an `alias` here in terms of scoping.\n\nAlternatively, you can also limit the scope to a dedicated block:\n\n```elixir\n  rewire Conversation, English: EnglishMock do   # (1) only rewired inside the block\n    stub(EnglishMock, :greet, fn -\u003e \"g'day\" end)\n    assert Conversation.start() == \"g'day!\"      # (2) test using the mock\n  end\n```\n\nPlus, you can also rewire module attributes.\n\n## FAQ\n\n**Will it work with `async: true`?**\n\nYes! Instead of overriding the module globally - like `meck` - it creates a _copy_ for each test.\n\n**Does it work with `mox`?**\n\nIt works great with [mox](https://github.com/dashbitco/mox) since `rewire` focuses on the _injection_ and doesn't care about where the _mock_ module comes from. `rewire` and `mox` are a great pair!\n\n**Will that slow down my tests?**\n\nMaybe just a little? Conclusive data from a larger code base isn't in yet.\n\n**Will test coverage be reported correctly?**\n\nYes!\n\n**Will it work with stateful processes?**\n\nIf the stateful process is started _after_ its module has been rewired, it will work fine. However, if the module is started _before_ - like a Phoenix controller - it won't work since it can't be rewired anymore. `rewire` is best used for unit tests.\n\n**Will it work with Erlang modules?**\n\nIt is not able to rewire Erlang modules - but you can replace Erlang module references in Elixir modules.\n\n**How does it deal with nested modules?**\n\nOnly the dependencies of the rewired module will be replaced. Any modules defined around the rewired module will be ignored. All references of the rewired module to them will be pointing to the original. You're always able to rewire them separately yourself.\n\n**How do I stop `mix format` from adding parentheses around `rewire`?**\n\nAdd this to your `.formatter.exs` file:\n\n```\nimport_deps: [:rewire]\n```\n\n**Why do I need this?**\n\nI haven't been happy with the existing tradeoffs of injecting dependencies into Elixir modules that allows me to alter their behavior in my unit tests.\n\nFor example, if you don't use `mox`, the best approach known to me is to pass-in dependencies via a function's parameters:\n\n```elixir\ndefmodule Conversation do\n  def start(mod \\\\ English), do: mod.greet()\nend\n```\n\nThe downsides to that approach are:\n\n1. Your application code is now littered with testing concerns.\n2. Navigation in your code editor doesn't work as well.\n3. Searches for usages of the module are more difficult.\n4. The compiler is not able to warn you in case `greet/0` doesn't exist on the `English` module.\n\nIf you use `mox` for your mocking, there's a slightly better approach:\n\n```elixir\ndefmodule Conversation do\n  def start(), do: english().greet()\n  defp english(), do: Application.get(:myapp, :english, English)\nend\n```\n\nIn this approach we use the app's config to replace a module with a `mox` mock during testing. This is a little better in my opinion, but still comes with most of the disadvantages described above.\n\n**Witchcraft! How does this work??**\n\nSimply put, `rewire` will create a copy of the module to rewire under a new name, replacing all hard-coded module references that should be changed in the process. Plus, it rewrites the test code in the `rewire` block to use the generated module instead.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanos%2Frewire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstephanos%2Frewire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanos%2Frewire/lists"}