{"id":13509218,"url":"https://github.com/archan937/mecks_unit","last_synced_at":"2025-04-25T18:32:10.006Z","repository":{"id":57522391,"uuid":"165037321","full_name":"archan937/mecks_unit","owner":"archan937","description":"A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library","archived":false,"fork":false,"pushed_at":"2020-04-19T13:08:49.000Z","size":45,"stargazers_count":55,"open_issues_count":1,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-13T19:46:22.760Z","etag":null,"topics":["asynchronous","elegant","elixir","exunit","meck","mocking"],"latest_commit_sha":null,"homepage":"http://elixirstatus.com/p/iXWw-mecksunit-elegantly-mock-module-functions-in-async-exunit-tests","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/archan937.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}},"created_at":"2019-01-10T10:11:32.000Z","updated_at":"2025-02-06T22:51:10.000Z","dependencies_parsed_at":"2022-08-28T17:33:56.223Z","dependency_job_id":null,"html_url":"https://github.com/archan937/mecks_unit","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archan937%2Fmecks_unit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archan937%2Fmecks_unit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archan937%2Fmecks_unit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/archan937%2Fmecks_unit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/archan937","download_url":"https://codeload.github.com/archan937/mecks_unit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250872271,"owners_count":21500792,"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":["asynchronous","elegant","elixir","exunit","meck","mocking"],"created_at":"2024-08-01T02:01:04.726Z","updated_at":"2025-04-25T18:32:09.701Z","avatar_url":"https://github.com/archan937.png","language":"Elixir","funding_links":[],"categories":["Testing"],"sub_categories":[],"readme":"# MecksUnit [![Build Status](https://travis-ci.org/archan937/mecks_unit.svg?branch=master)](https://travis-ci.org/archan937/mecks_unit)\n\nA simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's `:meck` library\n\n## Installation\n\nTo install MecksUnit, please do the following:\n\n  1. Add mecks_unit to your list of dependencies in `mix.exs`:\n\n      ```elixir\n      def deps do\n        [\n          {:mecks_unit, \"~\u003e 0.1.9\", only: :test}\n        ]\n      end\n      ```\n\n## Usage\n\nMocking module functions is pretty straightforward and done as follows:\n\n  1. Add `use MecksUnit.Case` at the beginning of your test file\n  2. Use `defmock` as if you would define the original module with `defmodule` containing mocked functions\n  3. Use `mocked_test` as if you would define a normal ExUnit `test` after having defined all the required mock modules\n  4. Add `MecksUnit.mock()` in your `test/test_helper.exs` file\n  5. Enjoy ;)\n\nPlease note that the defined mock modules only apply to the first `mocked_test` encountered.\nSo they are isolated (despite of `:meck` having an unfortunate global effect) as MecksUnit takes care of it.\nAlso, non-matching function heads within the mock module will result in invoking the original module function as well.\n\nAs of version `0.1.2`, you can assert function calls using `called` (returns a boolean) or `assert_called` (raises an\nerror when not having found a match) within your test block. Use `_` to match any argument as if you would pattern match.\n\nPrior to version `0.1.3`, you would very often get `:meck` related compile errors when using MecksUnit in multiple test files.\nFrom that version on, this problem is solved. Happy testing! ^^\n\n### Define mock module for entire test case\n\nAs of version `0.1.8`, you can \"preserve\" a mocked module definition for the rest of the test case by adding `preserve: true`.\n\n  ```elixir\n  defmock List, preserve: true do\n    def wrap(:foo), do: [1, 2, 3, 4]\n  end\n  ```\n\nThis behaviour is intended to be implemented as natural as possible. Therefore, you can override a preserved mock module once\njust by inserting a \"regular\" mock module definition:\n\n  ```elixir\n  defmock List, preserve: true do\n    def wrap(:foo), do: [1, 2, 3, 4]\n  end\n\n  # mocked tests ...\n\n  defmock List do\n    def wrap(:foo), do: [\"this only applies to the next `mocked_test`\"]\n  end\n  ```\n\nAlso, you can override a preserved mock module for the rest of the test case by using `preserve: true` again.\n\n  ```elixir\n  defmock List, preserve: true do\n    def wrap(:foo), do: [1, 2, 3, 4]\n  end\n\n  # mocked tests ...\n\n  defmock List do\n    def wrap(:foo), do: [\"this only applies to the next `mocked_test`\"]\n  end\n\n  # mocked tests ...\n\n  defmock List, preserve: true do\n    def wrap(:foo), do: [5, 6, 7, 8]\n  end\n  ```\n\nPlease note that this behaviour is also tested in [test/mecks_unit/preserve_test.exs](https://github.com/archan937/mecks_unit/blob/master/test/mecks_unit/preserve_test.exs).\n\n### A full example\n\nThe following is a working example defined in [test/mecks_unit_test.exs](https://github.com/archan937/mecks_unit/blob/master/test/mecks_unit_test.exs)\n\n  ```elixir\n  # (in test/test_helper.exs)\n\n  ExUnit.start()\n  MecksUnit.mock()\n  ```\n\n  ```elixir\n  # (in test/mecks_unit_test.exs)\n\n  defmodule Foo do\n    def trim(string) do\n      String.trim(string)\n    end\n  end\n\n  defmodule MecksUnitTest do\n    use ExUnit.Case, async: true\n    use MecksUnit.Case\n\n    defmock String do\n      def trim(\"  Paul  \"), do: \"Engel\"\n      def trim(\"  Foo  \", \"!\"), do: \"Bar\"\n      def trim(_, \"!\"), do: {:passthrough, [\"  Surprise!  !!!!\", \"!\"]}\n      def trim(_, _), do: :passthrough\n    end\n\n    defmock List do\n      def wrap(:foo), do: [1, 2, 3, 4]\n    end\n\n    mocked_test \"using mocked module functions\" do\n      task =\n        Task.async(fn -\u003e\n          assert \"Engel\" == String.trim(\"  Paul  \")\n          assert \"Engel\" == Foo.trim(\"  Paul  \")\n          assert \"Bar\" == String.trim(\"  Foo  \", \"!\")\n          assert \"  Surprise!  \" == String.trim(\"  Paul  \", \"!\")\n          assert \"MecksUnit\" == String.trim(\"  MecksUnit  \")\n          assert \"Paul Engel\" == String.trim(\"  Paul Engel  \", \" \")\n          assert [1, 2, 3, 4] == List.wrap(:foo)\n          assert [] == List.wrap(nil)\n          assert [:bar] == List.wrap(:bar)\n          assert [:foo, :bar] == List.wrap([:foo, :bar])\n          assert called List.wrap(:foo)\n          assert_called String.trim(_)\n        end)\n\n      Task.await(task)\n    end\n\n    test \"using the original module functions\" do\n      task =\n        Task.async(fn -\u003e\n          assert \"Paul\" == String.trim(\"  Paul  \")\n          assert \"Paul\" == Foo.trim(\"  Paul  \")\n          assert \"  Foo  \" == String.trim(\"  Foo  \", \"!\")\n          assert \"  Paul  \" == String.trim(\"  Paul  \", \"!\")\n          assert \"MecksUnit\" == String.trim(\"  MecksUnit  \")\n          assert \"Paul Engel\" == String.trim(\"  Paul Engel  \", \" \")\n          assert [:foo] == List.wrap(:foo)\n          assert [] == List.wrap(nil)\n          assert [:bar] == List.wrap(:bar)\n          assert [:foo, :bar] == List.wrap([:foo, :bar])\n        end)\n\n      Task.await(task)\n    end\n  end\n  ```\n\nPlease note that you can delegate to the original implementation by either returning `:passthrough` (which forwards the given arguments)\nor return a tuple `{:passthrough, arguments}` in which you can alter the arguments yourself.\n\n## Asynchronous testing\n\nUnlike [Mock](https://github.com/jjh42/mock), MecksUnit supports running mocked tests asynchronously. W00t! ^^\n\n## License\n\nCopyright (c) 2020 Paul Engel, released under the MIT License\n\nhttp://github.com/archan937 – http://twitter.com/archan937 – pm_engel@icloud.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farchan937%2Fmecks_unit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farchan937%2Fmecks_unit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farchan937%2Fmecks_unit/lists"}