Name: couchdb-meck
Owner: Cloudant
Description: Mirror of Apache CouchDB
Created: 2015-07-01 17:25:42.0
Updated: 2015-07-01 17:25:42.0
Pushed: 2015-10-22 06:37:28.0
Homepage: null
Size: 1244
Language: Erlang
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A mocking library for Erlang.
See what's new in 0.8 Release Notes.
non_strict
)no_link
)unstick
)Here's an example of using Meck in the Erlang shell:
ll V5.8.4 (abort with ^G)
eck:new(dog, [non_strict]). % non_strict is used to create modules that don't exist
eck:expect(dog, bark, fun() -> "Woof!" end).
og:bark().
f!"
eck:validate(dog).
eck:unload(dog).
og:bark().
xception error: undefined function dog:bark/0
Exceptions can be anticipated by Meck (resulting in validation still passing). This is intended to be used to test code that can and should handle certain exceptions indeed does take care of them:
eck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end).
atch dog:meow().
IT',{not_a_cat,[{meck,exception,2},
{meck,exec,4},
{dog,meow,[]},
{erl_eval,do_apply,5},
{erl_eval,expr,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]}}
eck:validate(dog).
Normal Erlang exceptions result in a failed validation. The following example is just to demonstrate the behavior, in real test code the exception would normally come from the code under test (which should, if not expected, invalidate the mocked module):
eck:expect(dog, jump, fun(Height) when Height > 3 ->
erlang:error(too_high);
(Height) ->
ok
end).
og:jump(2).
catch dog:jump(5).
IT',{too_high,[{meck,exec,4},
{dog,jump,[5]},
{erl_eval,do_apply,5},
{erl_eval,expr,5},
{shell,exprs,6},
{shell,eval_exprs,6},
{shell,eval_loop,3}]}}
meck:validate(dog).
e
Here's an example of using Meck inside an EUnit test case:
est() ->
meck:new(my_library_module),
meck:expect(my_library_module, fib, fun(8) -> 21 end),
?assertEqual(21, code_under_test:run(fib, 8)), % Uses my_library_module
?assert(meck:validate(my_library_module)),
meck:unload(my_library_module).
Pass-through is used when the original functionality of a module
should be kept. When the option passthrough
is used when calling
new/2
all functions in the original module will be kept in the
mock. These can later be overridden by calling expect/3
or
expect/4
.
ll V5.8.4 (abort with ^G)
eck:new(string, [unstick, passthrough]).
tring:strip(" test ").
t"
It's also possible to pass calls to the original function allowing us
to override only a certain behavior of a function (this usage is
compatible with the passthrough
option). passthrough/1
will always
call the original function with the same name as the expect is
defined in):
ll V5.8.4 (abort with ^G)
eck:new(string, [unstick]).
eck:expect(string, strip, fun(String) -> meck:passthrough([String]) end).
tring:strip(" test ").
t"
eck:unload(string).
tring:strip(" test ").
t"
Meck requires make
and rebar to build. To build Meck and run tests, go to the Meck
directory and simply type:
Two things might seem alarming when running the tests:
Both are expected due to the way Erlang currently prints errors. The
important line you should look for is All XX tests passed
, if that
appears all is correct.
Meck is best used via rebar. Add the following dependency t
your rebar.config
in your project root:
s, [
ck, ".*",
it, "https://github.com/eproxus/meck.git", {tag, "0.8"}}}
If you want to install your own built version of Meck add the ebin
directory to your Erlang code path or move the Meck folder into your
release folder and make sure that folder is in your ERL_LIBS
environment variable.
Patches are greatly appreciated! For a much nicer history, please
[write good commit messages][5]. Use a branch name prefixed by
feature/
(e.g. feature/my_example_branch
) for easier integration
when developing new features or fixes for meck.
Should you find yourself using Meck and have issues, comments or feedback please create an issue here on GitHub.
Contributors: