Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Elixir
  3. mock

mock

MITElixirv0.3.9

A mocking library for Elixir that uses the Erlang meck library to provide module mocking functionality for tests.

GitHubGitHub
691 stars82 forks0 contributors

What is mock?

Mock is a mocking library for the Elixir programming language that enables developers to create mock modules for testing purposes. It leverages the Erlang meck library and provides convenient Elixir macros to integrate mocking functionality seamlessly into test suites, allowing developers to isolate and test code with external dependencies.

Target Audience

Elixir developers writing unit and integration tests who need to mock external dependencies like HTTP clients, databases, or third-party APIs. It is particularly useful for developers working on test suites that require controlled environments to verify interactions with external modules.

Value Proposition

Developers choose Mock because it offers a clean, Elixir-native interface to the underlying Erlang meck library, making it easier to write and maintain tests with mocks. Its macros like `with_mock` and `test_with_mock` simplify test setup, and features like partial mocking with `:passthrough` and advanced assertion helpers provide flexibility and clarity in test code.

Overview

Mocking library for Elixir language

Use Cases

Best For

  • Testing Elixir applications that interact with external HTTP APIs by mocking HTTP client modules like HTTPotion.
  • Isolating database interactions in unit tests by mocking Ecto or database adapter modules.
  • Verifying function call patterns and frequencies in tests using assertion helpers like `assert_called` and `assert_called_exactly`.
  • Mocking multiple modules simultaneously in a single test with the `with_mocks` macro for complex dependency scenarios.
  • Partially mocking modules where only specific functions need to be mocked while others remain functional using the `:passthrough` option.
  • Creating mock functions that return different values based on input patterns, useful for simulating varied responses from external services.

Not Ideal For

  • Projects requiring asynchronous test execution with mocks, as Mock forces `async: false` due to global effects
  • Teams needing to mock internal function calls without refactoring code to use fully qualified names
  • Developers looking to mock macros directly, as Mock requires wrapper function workarounds
  • Applications that demand fine-grained mock behaviors beyond module-level replacement, where libraries like Mox might be more suitable

Pros & Cons

Pros

Clean Macro API

Offers Elixir-native macros like `with_mock` and `test_with_mock` that integrate seamlessly into ExUnit tests, simplifying mock setup and improving code readability as shown in the README examples.

Flexible Assertion Helpers

Provides a range of assertion macros such as `assert_called` with wildcard and pattern matching support via `:meck.is/1`, enabling precise verification of function calls without manual checks.

Partial Mocking Support

Includes the `:passthrough` option to mock specific functions while allowing access to other functions in the module, useful for isolating dependencies without full module replacement.

Input-Dependent Mocks

Allows defining mock functions with multiple clauses to return different values based on input patterns, making it easy to simulate varied responses from external services.

Cons

No Internal Call Mocking

Cannot mock functions called internally within the same module without refactoring to use fully qualified names, a limitation explicitly admitted in the README's 'NOT SUPPORTED' section.

Async Test Incompatibility

Mocking modules cannot be done asynchronously, requiring tests to run with `async: false`, which slows down test suites and limits parallelism in larger projects.

Macro Mocking Unsupported

Does not support mocking macros directly, forcing developers to create wrapper functions as a workaround, adding unnecessary complexity and boilerplate to tests.

Frequently Asked Questions

Quick Stats

Stars691
Forks82
Contributors0
Open Issues20
Last commit1 year ago
CreatedSince 2013

Tags

#elixir#test-isolation#unit-testing#meck#exunit#test-doubles#mocking#testing

Built With

m
meck
E
Erlang
E
Elixir

Included in

Elixir13.1k
Auto-fetched 1 day ago

Related Projects

ex_machinaex_machina

Create test data for Elixir applications

Stars2,061
Forks141
Last commit21 days ago
wallabywallaby

Concurrent browser tests for your Elixir web apps.

Stars1,805
Forks212
Last commit2 months ago
moxmox

Mocks and explicit contracts in Elixir

Stars1,400
Forks79
Last commit25 days ago
houndhound

Elixir library for writing integration tests and browser automation

Stars1,368
Forks139
Last commit2 years ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub