How edgurgel/mimic works
Project type and purpose
• This is an open-source Elixir library (“Mimic”) that provides a mocking DSL and runtime for tests.
• It hooks into existing modules by copying their BEAM code at test time, intercepts calls, and routes them through a per-process ETS table and a central server to apply stubs, expectations, rejections, and type checks.
File structure
• Top‐level mix files (mix.exs, mix.lock) and config (.credo.exs, .formatter.exs) indicate a standard Elixir OTP library.
• lib/mimic/application.ex defines the application/ supervision tree.
• lib/mimic/server.ex implements a GenServer or similar process to manage ETS tables and routing.
• lib/mimic/dsl.ex provides a DSL wrapper.
• lib/mimic/type_check.ex integrates optional Ham-based type validation.
• lib/mimic/module.ex, cover.ex define core helpers.
• lib/mimic/*.ex contain error types (UnexpectedCallError, VerificationError) and control modules.
• Tests under test/ drive the behavior via ExUnit.
README for additional insights
• Describes how Mimic.copy/1 creates a “copied” version of a module and installs run-time hooks.
• All calls to a copied module go first through an ETS lookup (per‐pid) to decide whether to call a stub, an expected function, or original implementation (or raise).
• verify!/1 and ExUnit integration enforce that expectations are met.
Elements
• “copy/2” from Test → Server
• “write stub/expect” from Test → ETS
• “foo/arity call” from Test → Proxy Module → ETS/Server
• “invoke stub/fn” or “invoke original”
• “record call” back into ETS
• “verify!” or automatic end-of-test → Server → compare expectations
• Green = user/test processes
• Blue = core engine (Server + ETS)
• Orange = proxy modules & code-injection
• Grey = optional TypeCheck plugin
Details
• Separation between proxy (injected) modules and original modules.
• Essential touches: supervision, ETS lookup, code injection, DSL vs. core calls.