double alternatives and similar packages
Based on the "Testing" category.
Alternatively, view double alternatives based on common mentions on social networks and blogs.
-
proper
PropEr: a QuickCheck-inspired property-based testing tool for Erlang -
bypass
Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests. -
amrita
A polite, well mannered and thoroughly upstanding testing framework for Elixir -
power_assert
Power Assert in Elixir. Shows evaluation results each expression. -
shouldi
Elixir testing libraries with nested contexts, superior readability, and ease of use -
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 -
Stubr
Stubr is a set of functions helping people to create stubs and spies in Elixir. -
mix_test_interactive
Interactive watch mode for Elixir's mix test. https://hexdocs.pm/mix_test_interactive/ -
mecks_unit
A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library -
test_selector
Elixir library to help selecting the right elements in your tests. -
factory_girl_elixir
Minimal implementation of Ruby's factory_girl in Elixir. -
toxiproxy_ex
ToxiproxyEx is an Elixir API client for the resilience testing tool Toxiproxy. -
ex_parameterized
This library support parameterized test with test_with_params macro. -
cobertura_cover
Output test coverage information in Cobertura-compatible format -
ex_unit_fixtures
A library for defining modular dependencies (fixtures) for ExUnit tests. -
ElixirMock
Creates clean, concurrent, inspectable mocks from elixir modules
Clean code begins in your IDE with SonarLint
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