double alternatives and similar packages
Based on the "Testing" category.
Alternatively, view double alternatives based on common mentions on social networks and blogs.
-
ex_machina
Flexible test factories for Elixir. Works out of the box with Ecto and Ecto associations. -
wallaby
Wallaby helps test your web applications by simulating user interactions concurrently and manages browsers. -
proper
PropEr (PROPerty-based testing tool for ERlang) is a QuickCheck-inspired open-source property-based testing tool for Erlang. -
bypass
Bypass provides a quick way to create a mock HTTP server with a custom plug. -
mix_test_watch
Automatically run your Elixir project's tests each time you save a file. -
amrita
A polite, well mannered and thoroughly upstanding testing framework for Elixir. -
ponos
Ponos is an Erlang application that exposes a flexible load generator API. -
power_assert
Power Assert in Elixir. Shows evaluation results each expression. -
shouldi
Elixir testing libraries with nested contexts, superior readability, and ease of use. -
chaperon
An HTTP service performance & load testing framework written in Elixir. -
katt
KATT (Klarna API Testing Tool) is an HTTP-based API testing tool for Erlang. -
FakeServer
FakeServer integrates with ExUnit to make external APIs testing simpler -
mecks_unit
A package to elegantly mock module functions within (asynchronous) ExUnit tests using meck. -
factory_girl_elixir
Minimal implementation of Ruby's factory_girl in Elixir. -
test_selector
A set of test helpers that make sure you always select the right elements in your Phoenix app. -
cobertura_cover
Writes a coverage.xml from mix test --cover file compatible with Jenkins' Cobertura plugin. -
mix_test_interactive
Interactive test runner for mix test with watch mode. -
ex_unit_fixtures
A library for defining modular dependencies for ExUnit tests. -
ElixirMock
(alpha) Sanitary mock objects for elixir, configurable per test and inspectable -
ExopData
A library that helps you to write property-based tests by providing a convenient way to define complex custom data generators.
Scout APM - Leading-edge performance monitoring starting at $39/month
Do you think we are missing an alternative of double or a related project?
README
Double
Double builds on-the-fly injectable dependencies for your tests. It does NOT override behavior of existing modules or functions. Double uses Elixir's built-in language features such as pattern matching and message passing to give you everything you would normally need a complex mocking tool for.
Installation
The package can be installed as:
- Add
double
to your list of dependencies inmix.exs
:
def deps do
[{:double, "~> 0.7.0", only: :test}]
end
Usage
Start Double in your test/test_helper.exs
file:
ExUnit.start
Application.ensure_all_started(:double)
Module/Behaviour Doubles
Double creates a fake module based off of a behaviour or module. You can use this module like any other module that you call functions on. Each stub you define will verify that the function name and arity are defined in the target module or behaviour.
defmodule Example do
def process(io \\ IO) do # allow an alternative dependency to be passed
io.puts("It works without mocking libraries!")
end
end
defmodule ExampleTest do
use ExUnit.Case
import Double
test "example outputs to console" do
io_stub = stub(IO,:puts, fn(_msg) -> :ok end)
Example.process(io_stub) # inject the stub module
# use built-in ExUnit assert_receive/refute_receive to verify things
assert_receive({IO, :puts, ["It works without mocking libraries!"]})
end
end
Features
Basics
# Stub a function
dbl = stub(ExampleModule, :add, fn(x, y) -> x + y end)
dbl.add(2, 2) # 4
# Pattern match arguments
dbl = stub(Application, :ensure_all_started, fn(:logger) -> nil end)
dbl.ensure_all_started(:logger) # nil
dbl.ensure_all_started(:something) # raises FunctionClauseError
# Stub as many functions as you want
dbl = ExampleModule
|> stub(:add, fn(x, y) -> x + y end)
|> stub(:subtract, fn(x, y) -> x - y end)
Different return values for different arguments
dbl = ExampleModule
|> stub(:example, fn("one") -> 1 end)
|> stub(:example, fn("two") -> 2 end)
|> stub(:example, fn("three") -> 3 end)
dbl.example("one") # 1
dbl.example("two") # 2
dbl.example("three") # 3
Multiple calls returning different values
dbl = ExampleModule
|> stub(:example, fn("count") -> 1 end)
|> stub(:example, fn("count") -> 2 end)
dbl.example("count") # 1
dbl.example("count") # 2
dbl.example("count") # 2
Exceptions
dbl = ExampleModule
|> stub(:example_with_error_type, fn -> raise RuntimeError, "kaboom!" end)
|> stub(:example_with_error_type, fn -> raise "kaboom!" end)
Verifying calls
If you want to verify that a particular stubbed function was actually executed,
Double ensures that a message is receivable to your test process so you can just use the built-in ExUnit assert_receive/assert_received
.
The message is a 3-tuple {module, :function, [arg1, arg2]}
and .
dbl = ExampleModule
|> stub(:example, fn("count") -> 1 end)
dbl.example("count")
assert_receive({ExampleModule, :example, ["count"]})
Remember that pattern matching is your friend so you can do all kinds of neat tricks on these messages.
assert_receive({ExampleModule, :example, ["c" <> _rest]}) # verify starts with "c"
assert_receive({ExampleModule, :example, [%{test: 1}]) # pattern match map arguments
assert_receive({ExampleModule, :example, [x]}) # assign an argument to x to verify another way
assert x == "count"
Module Verification
By default your setups will check the source module to ensure the function exists with the correct arity.
stub(IO, :non_existent_function, fn(x) -> x end) # raises VerifyingDoubleError
Clearing Stubs
Occasionally it's useful to clear the stubs for an existing double. This is useful when you have a shared setup and a test needs to change the way a double is stubbed without recreating the whole thing.
dbl = IO
|> stub(:puts, fn(_) -> :ok end)
|> stub(:inspect, fn(_) -> :ok end)
# later
dbl |> clear(:puts) # clear an individual function
dbl |> clear([:puts, :inspect]) # clear a list of functions
dbl |> clear() # clear all functions