stubr v1.3.5 Stubr
Provides module stubs for Elixir.
Module stubs can be created using stub!/1
and stub!/2
.
The input to this function is a keyword list of function names
(expressed as atoms) and their implementations (expressed as
anonymous functions):
[function_name: (...) -> any()]
Additionally, takes an optional keyword list to configure the stub.
Options
The options available to stub!/2
are:
:module
- when set, if the module does not contain a function defined in the keyword list, then raises anUndefinedFunctionError
:auto_stub
- when true and a module has been set, if there is not a matching function, then defers to the module function (defaults tofalse
)behaviour
- when set, raises a warning if the stub does not implement the behaviourcall_info
- when set, if a function is called, records the input and the output to the function. Accessed by calling__stubr__(:call_info: :function_name)
(defaults tofalse
)
Summary
Functions
Returns number of times the function was called. The
call_info
option must be set to true
Returns number of times the function was called for a particular
arguement. The call_info
option must be set to true
Returns the call info of a stubbed module
Returns true if the stubbed function was called. The
call_info
option must be set to true
Returns true if the invocation of the anonymous function returns
true when applied to the arguments of at least one function call. The
call_info
option must be set to true
Returns true if the stubbed function is called with a particular argument. The
call_info
option must be set to true
Recieves a keyword list of function names and anonymous functions where all calls by the stub to a function in this list are deferred to the invocation of the anonymous function
Functions
Returns number of times the function was called. The
call_info
option must be set to true
.
Examples
iex> stubbed_functions = [foo: fn(_) -> :ok end, foo: fn(_, _) -> :ok end]
iex> stub = Stubr.stub!(stubbed_functions, call_info: true)
iex> stub.foo(:baz)
iex> stub.foo(:baz)
iex> stub.foo(:baz, :qux)
iex> stub |> Stubr.call_count(:foo)
3
Returns number of times the function was called for a particular
arguement. The call_info
option must be set to true
.
Examples
iex> stubbed_functions = [foo: fn(_) -> :ok end, foo: fn(_, _) -> :ok end]
iex> stub = Stubr.stub!(stubbed_functions, call_info: true)
iex> stub.foo(:baz)
iex> stub.foo(:baz)
iex> stub.foo(:baz, :qux)
iex> stub |> Stubr.call_count(:foo, [:baz])
2
iex> stub |> Stubr.call_count(:foo, [:baz, :qux])
1
Returns the call info of a stubbed module.
Examples
iex> uniform_stub = [uniform: fn(_) -> 3 end]
iex> rand_stub = Stubr.stub!(uniform_stub, module: :rand, call_info: true)
iex> rand_stub.uniform(2)
3
iex> Stubr.call_info!(rand_stub, :uniform)
[%{input: [2], output: 3}]
Returns true if the stubbed function was called. The
call_info
option must be set to true
.
Examples
iex> stubbed_functions = [foo: fn(_) -> :ok end, bar: fn(_) -> :ok end]
iex> stub = Stubr.stub!(stubbed_functions, call_info: true)
iex> stub.foo(:baz)
iex> stub |> Stubr.called?(:foo)
true
iex> stub |> Stubr.called?(:bar)
false
Returns true if the invocation of the anonymous function returns
true when applied to the arguments of at least one function call. The
call_info
option must be set to true
.
Examples
iex> stubbed_function = [foo: fn(_) -> :ok end]
iex> stub = Stubr.stub!(stubbed_function, call_info: true)
iex> stub.foo(%{bar: :ok, baz: :error})
iex> stub |> Stubr.called_match?(:foo, fn [arg] -> Map.has_key?(arg, :bar) end)
true
iex> stub |> Stubr.called_match?(:foo, fn [arg] -> arg.baz == :ok end)
false
Returns true if the stubbed function is called with a particular argument. The
call_info
option must be set to true
.
Examples
iex> stubbed_function = [foo: fn(_, _) -> :ok end]
iex> stub = Stubr.stub!(stubbed_function, call_info: true)
iex> stub.foo(:bar, :baz)
iex> stub |> Stubr.called_with?(:foo, [:bar, :baz])
true
iex> stub |> Stubr.called_with?(:foo, [:qux])
false
Recieves a keyword list of function names and anonymous functions where all calls by the stub to a function in this list are deferred to the invocation of the anonymous function.
Options
:module
- when set, if the module does not contain a function defined in the keyword list, then raises anUndefinedFunctionError
:auto_stub
- when true and a module has been set, if there is not a matching function, then defers to the module. (defaults tofalse
)behaviour
- when set, raises a warning if the stub does not implement the behaviourcall_info
- when set, if a function is called, records the input and the output to the function. Accessed by calling__stubr__(:call_info: :function_name)
(defaults tofalse
)
Examples
iex> uniform_stub = [uniform: fn(_) -> 3 end]
iex> rand_stub = Stubr.stub!(uniform_stub, module: :rand)
iex> rand_stub.uniform(2)
3
iex> rand_stub.uniform(4)
3