{"id":19866135,"url":"https://github.com/tlux/delx","last_synced_at":"2025-05-02T05:32:22.475Z","repository":{"id":57489156,"uuid":"186834036","full_name":"tlux/delx","owner":"tlux","description":"Defdelegate on steroids! An Elixir library to make function delegation testable.","archived":false,"fork":false,"pushed_at":"2022-11-07T15:06:08.000Z","size":60,"stargazers_count":3,"open_issues_count":2,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-06T23:08:40.699Z","etag":null,"topics":["assertions","defdelegate","delegated-functions","elixir","elixir-library","hex-package","mox"],"latest_commit_sha":null,"homepage":"","language":"Elixir","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/tlux.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}},"created_at":"2019-05-15T13:38:33.000Z","updated_at":"2023-02-08T20:25:09.000Z","dependencies_parsed_at":"2023-01-22T15:30:26.230Z","dependency_job_id":null,"html_url":"https://github.com/tlux/delx","commit_stats":null,"previous_names":["i22-digitalagentur/delx"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Fdelx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Fdelx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Fdelx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Fdelx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlux","download_url":"https://codeload.github.com/tlux/delx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251993002,"owners_count":21677022,"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":["assertions","defdelegate","delegated-functions","elixir","elixir-library","hex-package","mox"],"created_at":"2024-11-12T15:25:04.730Z","updated_at":"2025-05-02T05:32:17.467Z","avatar_url":"https://github.com/tlux.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Delx\n\n[![Build Status](https://travis-ci.org/tlux/delx.svg?branch=master)](https://travis-ci.org/tlux/delx)\n[![Coverage Status](https://coveralls.io/repos/github/tlux/delx/badge.svg?branch=master)](https://coveralls.io/github/tlux/delx?branch=master)\n[![Hex.pm](https://img.shields.io/hexpm/v/delx.svg)](https://hex.pm/packages/delx)\n\n[Defdelegate](https://hexdocs.pm/elixir/Kernel.html#defdelegate/2) on steroids!\nAn Elixir library to make function delegation testable.\n\n## Prerequisites\n\n- Erlang 20 or greater\n- Elixir 1.8 or greater\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `delx` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:delx, \"~\u003e 3.0\"}\n  ]\nend\n```\n\n## Usage\n\nCheck out the full docs at [https://hexdocs.pm/delx](https://hexdocs.pm/delx).\n\nLet's say you have the following module.\n\n```elixir\ndefmodule Greeter.StringGreeter do\n  def hello(name) do\n    \"Hello, #{name}!\"\n  end\nend\n```\n\nYou can delegate functions calls to another module by using the `Delx` module\nand calling the `defdelegate/2` macro in the module body. It has the same API as\nElixir's own `Kernel.defdelegate/2` macro.\n\n```elixir\ndefmodule Greeter do\n  use Delx, otp_app: :greeter\n\n  defdelegate hello(name), to: Greeter.StringGreeter\nend\n\nGreeter.hello(\"Tobi\")\n# =\u003e \"Hello, Tobi!\"\n```\n\n## Testing\n\nOne great benefit of Delx is that you can test delegation without invoking\nthe actual implementation of the delegation target, thus eliminating all side\neffects.\n\n### Built-In Assertions\n\nDelx brings it's own test assertions.\n\nAll you need to do is to activate delegation mocking for your test environment\nby putting the following line in your `config/test.exs`:\n\n```elixir\nconfig :greeter, Delx, mock: true\n```\n\nThen in your tests, you can import `Delx.TestAssertions` and use the\n`assert_delegate/2` and `refute_delegate/2` assertions.\n\n```elixir\ndefmodule GreeterTest do\n  use ExUnit.Case\n\n  import Delx.TestAssertions\n\n  describe \"hello/1\" do\n    test \"delegate to Greeter.StringGreeter\" do\n      assert_delegate {Greeter, :hello, 1}, to: Greeter.StringGreeter\n    end\n  end\nend\n```\n\nNote that once you activate mocking all delegated functions do not return\nanymore but instead raise the `Delx.MockedDelegationError`. If you really\nwant to call the original implementation, you have to avoid any calls of\ndelegated functions.\n\n### With Mox\n\nIf you are using [Mox](https://hexdocs.pm/mox) in your application you have\nanother possibility to test delegated functions.\n\nRegister a mock for the `Delx.Delegator` behavior to your\n`test/test_helper.exs` (or wherever you define your mocks):\n\n```elixir\nMox.defmock(Greeter.DelegatorMock, for: Delx.Delegator)\n```\n\nThen, in your `config/test.exs` you have to set the mock as delegator module\nfor your app.\n\n```elixir\nconfig :my_app, Delx, delegator: Greeter.DelegatorMock\n```\n\nPlease make sure not to use the `:mock` option and a `:delegator` option at the\nsame time as this may lead to unexpected behavior.\n\nNow you are able to `expect` calls to delegated functions:\n\n```elixir\ndefmodule GreeterTest do\n  use ExUnit.Case\n\n  import Mox\n\n  setup :verify_on_exit!\n\n  describe \"hello/1\" do\n    test \"delegate to Greeter.StringGreeter\" do\n      expect(\n        Greeter.DelegatorMock,\n        :apply,\n        fn {Greeter, :hello},\n           {Greeter.StringGreeter, :hello},\n           [\"Tobi\"] -\u003e\n          :ok\n        end\n      )\n\n      Greeter.hello(\"Tobi\")\n    end\n  end\nend\n```\n\nFor more information on how to implement your own delegator, refer to the\ndocs of the `Delx.Delegator` behavior.\n\nNote that the configuration is only applied at compile time, so you are unable\nto mock or replace the delegator module at runtime.\n\n## Migration from v2 to v3\n\nNote that the function `defdel/2` has been removed in favor of `defdelegate/2`\nto give a better experience with the tooling. Additionally, let's say you want\nto migrate away from Delx you can simply un`use` Delx to fall back to the\ndefault `Kernel.defdelegate/2` and your code will still work.\n\nThe config option to enable testing mode has changed from `stub` to `mock`. If\nyou got the following line in your config:\n\n```elixir\nconfig :greeter, Delx, stub: true\n```\n\nPlease change it to:\n\n```elixir\nconfig :greeter, Delx, mock: true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlux%2Fdelx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlux%2Fdelx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlux%2Fdelx/lists"}