commit | de9a19cb7c20539e71b074692bd519311a3c3d09 | [log] [tgz] |
---|---|---|
author | Adam Lindberg <eproxus@gmail.com> | Wed Mar 30 10:19:53 2011 +0200 |
committer | Adam Lindberg <eproxus@gmail.com> | Wed Mar 30 10:19:53 2011 +0200 |
tree | 72ab83842743352621bd627d2879e97dbe8991b4 | |
parent | bfc8354085172231a5207140658ef8a4aaab5646 [diff] |
Add test to search complete history
A mocking library for Erlang.
With meck you can easily mock modules in Erlang. Since meck is intended to be used in testing, you can also perform some basic validations on the mocked modules, such as making sure no function is called in a way it should not.
meck automatically renames existing modules in case they are loaded when you want to mock them, and restores them upon unloading of the mocked module. It is also possible to call the original functions from a mocked module using meck:passthrough/1
from inside an expectation.
Here's an example of using meck in the Erlang shell:
Eshell V5.7.5 (abort with ^G) 1> meck:new(dog). ok 2> meck:expect(dog, bark, fun() -> "Woof!" end). ok 3> dog:bark(). "Woof!" 4> meck:validate(dog). true 5> meck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end). ok 6> catch dog:meow(). {'EXIT',{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}]}} 7> meck:validate(dog). true 8> meck:expect(dog, jump, fun(Height) when Height > 3 -> erlang:error(too_high); (Height) -> ok end). ok 9> dog:jump(2). ok 10> catch dog:jump(5). {'EXIT',{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}]}} 11> meck:validate(dog). false
Here's an example of using meck inside an EUnit test case:
my_test() -> meck:new(library_module), meck:expect(library_module, fib, fun(8) -> 21 end), ?assertEqual(21, code_under_test:run(fib, 8)), ?assert(meck:validate(library_module)).
Pass-through in action:
Eshell V5.7.5 (abort with ^G) 1> code:unstick_mod(string). true 2> meck:new(string). ok 3> meck:expect(string, strip, fun(String) -> meck:passthrough([String]) end). ok 4> string:strip(" test "). "test" 5> meck:unload(string). ok 6> string:strip(" test "). "test"
Should you find yourself using meck and have issues, comments or feedback please [create an issue!] 1