Description
ExStub provides an easy way to stub a module and record the function calls on it.
ExStub alternatives and similar packages
Based on the "Testing" category.
Alternatively, view ExStub 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. -
Stubr
Stubr is a set of functions helping people to create stubs and spies in Elixir. -
FakeServer
FakeServer integrates with ExUnit to make external APIs testing simpler -
mecks_unit
A simple Elixir package to elegantly mock module functions within (asynchronous) ExUnit tests using Erlang's :meck library -
mix_test_interactive
Interactive watch mode for Elixir's mix test. https://hexdocs.pm/mix_test_interactive/ -
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
Build time-series-based applications quickly and at scale.
Do you think we are missing an alternative of ExStub or a related project?
Popular Comparisons
README
ExStub
ExStub
provides an easy way to stub a module and record the function calls on it.
Installation
Add ex_stub
to your deps in mix.exs
as a development dependency.
def deps do
[{:ex_stub, "~> 0.1.0", only: :test}]
end
Usage
If you have a module in your original application like:
defmodule OriginalModule do
def process(param), do: :original_process
def another_method, do: :original_method
end
You can quickly create a stub copy of this module using defstub
use ExStub
defstub MyStub, for: OriginalModule do
def process(true), do: :stubbed1
def process(false), do: :stubbed2
def process(1), do: :stubbed3
end
Now you can pass around MyStub
instead of OriginalModule
.
When you invoke method from the created MyStub
, if the method was stubbed it will call the stubbed version.
Else the original version will be called.
MyStub.process(true) # returns :stubbed1
MyStub.process(false) # returns :stubbed2
MyStub.process(1) # returns :stubbed3
MyStub.process(20) # returns :original_process
MyStub.another_method # returns :original_method
Notice that Since we did not stub another_method
, calling it on MyStub
returns the original implementation.
Also when calling MyStub.process(20)
the original implementation is called since it failed pattern matching with our stub version of the method.
As a safety procedure, if you try to stub a method that is not found in the original module. ExStub will throw a compilation error telling you about the unexpected stubbed method.
defstub MyStub, for: OriginalModule do
def new_method(), do: :stubbed1
end
The following error will be thrown
** (RuntimeError) Cannot provide implementations for methods that are not in the original module
The def `{:new_method, 0}` is not defined in module `OriginalModule`
Recording method calls
All the functions called on the defstub
created module will be recorded.
To get all the functions calls on YourModule
module
ExStub.Recorder.calls(YourModule)
To get all the :the_method
function calls on YourModule
ExStub.Recorder.calls(YourModule, :the_method)
Alternativey, you can use assert_called
in your unit tests:
The syntax is assert_called ModuleName.function_name(params)
# No parameters
assert_called ModuleName.function_name
# nil passed
assert_called ModuleName.function_name(nil)
# multiple parameters
assert_called ModuleName.function_name(1, 2)
Some more examples
MyStub.process(1)
# Passes since we called the function with [1]
assert_called MyStub.process(1)
# Fails since the parameters dont match
assert_called MyStub.process(1, 2)
# Fails since we did not call `another_method`
assert_called MyStub.another_method