{"id":13509234,"url":"https://github.com/dashbitco/mox","last_synced_at":"2025-05-14T02:08:48.197Z","repository":{"id":38323603,"uuid":"104735813","full_name":"dashbitco/mox","owner":"dashbitco","description":"Mocks and explicit contracts in Elixir","archived":false,"fork":false,"pushed_at":"2024-08-19T17:34:53.000Z","size":146,"stargazers_count":1343,"open_issues_count":4,"forks_count":77,"subscribers_count":19,"default_branch":"main","last_synced_at":"2024-10-29T15:04:33.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","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/dashbitco.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-09-25T10:24:09.000Z","updated_at":"2024-10-26T00:16:18.000Z","dependencies_parsed_at":"2023-01-19T15:46:18.461Z","dependency_job_id":"13253356-8284-4ca4-ae88-86380f680160","html_url":"https://github.com/dashbitco/mox","commit_stats":{"total_commits":122,"total_committers":43,"mean_commits":"2.8372093023255816","dds":0.680327868852459,"last_synced_commit":"a59a1840676ceb0ef6c3e0f4959242ccb5d593ab"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fmox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fmox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fmox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fmox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dashbitco","download_url":"https://codeload.github.com/dashbitco/mox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254053202,"owners_count":22006717,"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":[],"created_at":"2024-08-01T02:01:04.948Z","updated_at":"2025-05-14T02:08:43.182Z","avatar_url":"https://github.com/dashbitco.png","language":"Elixir","funding_links":[],"categories":["Testing","Elixir"],"sub_categories":[],"readme":"# Mox\n\n[![hex.pm](https://img.shields.io/badge/Package%20on%20hex.pm-informational)](https://hex.pm/packages/mox)\n[![hexdocs.pm](https://img.shields.io/badge/Documentation-ff69b4)](https://hexdocs.pm/mox)\n[![ci](https://github.com/dashbitco/mox/actions/workflows/ci.yml/badge.svg)](https://github.com/dashbitco/mox/actions/workflows/ci.yml)\n[![coverage](https://coveralls.io/repos/github/dashbitco/mox/badge.svg?branch=main)](https://coveralls.io/github/dashbitco/mox?branch=main)\n\nMox is a library for defining concurrent mocks in Elixir.\n\nThe library follows the principles outlined in [\"Mocks and explicit contracts\"](https://dashbit.co/blog/mocks-and-explicit-contracts), summarized below:\n\n  1. No ad-hoc mocks. You can only create mocks based on behaviours\n\n  2. No dynamic generation of modules during tests. Mocks are preferably defined in your `test_helper.exs` or in a `setup_all` block and not per test\n\n  3. Concurrency support. Tests using the same mock can still use `async: true`\n\n  4. Rely on pattern matching and function clauses for asserting on the\n     input instead of complex expectation rules\n\nThe goal behind Mox is to help you think and define the contract between the different parts of your application. In the opinion of Mox maintainers, as long as you follow those guidelines and keep your tests concurrent, any library for mocks may be used (or, in certain cases, you may not even need one).\n\n[See the documentation](https://hexdocs.pm/mox) for more information.\n\n## Installation\n\nJust add `mox` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:mox, \"~\u003e 1.0\", only: :test}\n  ]\nend\n```\n\nMox should be automatically started unless the `:applications` key is set inside `def application` in your `mix.exs`. In such cases, you need to [remove the `:applications` key in favor of `:extra_applications`](https://elixir-lang.org/blog/2017/01/05/elixir-v1-4-0-released/#application-inference) or call `Application.ensure_all_started(:mox)` in your `test/test_helper.exs`.\n\n## Basic Usage\n\n### 1) Add behaviour, defining the contract\n\n```elixir\n# lib/weather_behaviour.ex\ndefmodule WeatherBehaviour do\n  @callback get_weather(binary()) :: {:ok, map()} | {:error, binary()}\nend\n```\n\n### 2) Add implementation for the behaviour\n\n```elixir\n# lib/weather_impl.ex\ndefmodule WeatherImpl do\n  @moduledoc \"\"\"\n  An implementation of a WeatherBehaviour\n  \"\"\"\n\n  @behaviour WeatherBehaviour\n\n  @impl WeatherBehaviour\n  def get_weather(city) when is_binary(city) do\n    # Here you could call an external api directly with an HTTP client or use a third\n    # party library that does that work for you. In this example we send a\n    # request using a `httpc` to get back some html, which we can process later.\n\n    :inets.start()\n    :ssl.start()\n\n    case :httpc.request(:get, {\"https://www.google.com/search?q=weather+#{city}\", []}, [], []) do\n      {:ok, {_, _, html_content}} -\u003e {:ok, %{body: html_content}}\n      error -\u003e {:error, \"Error getting weather: #{inspect(error)}\"}\n    end\n  end\nend\n```\n\n### 3) Add a switch\n\nThis can pull from your `config/config.exs`, `config/test.exs`, or, you can have no config as shown below and rely on a default. We also add a function to a higher level abstraction that will call the correct implementation:\n\n```elixir\n# bound.ex, the main context we chose to call this function from\ndefmodule Bound do\n  def get_weather(city) do\n    weather_impl().get_weather(city)\n  end\n\n  defp weather_impl() do\n    Application.get_env(:bound, :weather, WeatherImpl)\n  end\nend\n```\n\n### 4) Define the mock so it is used during tests\n\n```elixir\n# In your test/test_helper.exs\nMox.defmock(WeatherBehaviourMock, for: WeatherBehaviour) # \u003c- Add this\nApplication.put_env(:bound, :weather, WeatherBehaviourMock) # \u003c- Add this\n\nExUnit.start()\n```\n\n### 5) Create a test and use `expect` to assert on the mock arguments\n\n```elixir\n# test/bound_test.exs\ndefmodule BoundTest do\n  use ExUnit.Case\n\n  import Mox\n\n  setup :verify_on_exit!\n\n  describe \"get_weather/1\" do\n    test \"fetches weather based on a location\" do\n      expect(WeatherBehaviourMock, :get_weather, fn args -\u003e\n        # here we can assert on the arguments that get passed to the function\n        assert args == \"Chicago\"\n\n        # here we decide what the mock returns\n        {:ok, %{body: \"Some html with weather data\"}}\n      end)\n\n      assert {:ok, _} = Bound.get_weather(\"Chicago\")\n    end\n  end\nend\n```\n\n## Enforcing consistency with behaviour typespecs\n\n[Hammox](https://github.com/msz/hammox) is an enhanced version of Mox which automatically makes sure that calls to mocks match the typespecs defined in the behaviour. If you find this useful, see the [project homepage](https://github.com/msz/hammox).\n\n## License\n\nCopyright 2017 Plataformatec \\\nCopyright 2020 Dashbit\n\n  Licensed under the Apache License, Version 2.0 (the \"License\");\n  you may not use this file except in compliance with the License.\n  You may obtain a copy of the License at\n\n      http://www.apache.org/licenses/LICENSE-2.0\n\n  Unless required by applicable law or agreed to in writing, software\n  distributed under the License is distributed on an \"AS IS\" BASIS,\n  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  See the License for the specific language governing permissions and\n  limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdashbitco%2Fmox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdashbitco%2Fmox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdashbitco%2Fmox/lists"}