Meck at erlang factory, london 2011
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Meck at erlang factory, london 2011

  • 2,698 views
Uploaded on

Meck is a fairly new mocking library for Erlang that enables you to easily mock Erlang modules during testing. You can create modules, add functions, specify return values and throw exceptions. You......

Meck is a fairly new mocking library for Erlang that enables you to easily mock Erlang modules during testing. You can create modules, add functions, specify return values and throw exceptions. You can also look at a history of the calls made to the module, and make assertions based on that history. Meck can be used for any kind of testing, but is particularly useful in unit or component testing where it is often difficult to handle dependencies on external components or libraries. Meck has been used in production in many different projects for about a year.

This talk gives you an overview of the Meck API and looks at how meck is used and works.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,698
On Slideshare
2,533
From Embeds
165
Number of Embeds
5

Actions

Shares
Downloads
19
Comments
0
Likes
1

Embeds 165

http://speakerrate.com 157
https://twitter.com 4
http://www.linkedin.com 2
http://duckduckgo.com 1
https://www.linkedin.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Erlang Solutions Ltd.MeckA Mocking Library for Erlang © 2011 Erlang Solutions Ltd.
  • 2. What Is Mocking? adjective /mäk/  1. Not authentic or real, but without the intention to deceive © 2011 Erlang Solutions Ltd.
  • 3. What Is Mocking? • Dummy objects • Fake objects • Stubs • Mockshttp://martinfowler.com/articles/mocksArentStubs.html © 2011 Erlang Solutions Ltd.
  • 4. What Is Mocking? • Dummy objects • Fake objects • Stubs } meck • Mockshttp://martinfowler.com/articles/mocksArentStubs.html © 2011 Erlang Solutions Ltd.
  • 5. Alternative Ways To Mock test() -> % Setup mock F = "test/mock.erl", {ok, M, B} = compile:file(F, [binary]), code:load_binary(M, F, B), % Run tests ?assert(system_under_test:run()), code:purge(mock). © 2011 Erlang Solutions Ltd.
  • 6. Other Mocking Libraries• erlymock - https://github.com/sheyll/erlymock Unmaintained?• emock - https://github.com/noss/emock• effigy - https://github.com/cliffmoon/effigy © 2011 Erlang Solutions Ltd.
  • 7. Create a Module 1> meck:new(test). ok 2> test:module_info(). [{exports,[{module_info,0}, {module_info,1}]}, {imports,[]}, {attributes,[{vsn,[276740...]}]}, {compile,[{options,[]}, {version,"4.7.4"}, {time,{2011,5,30,11,48,5}}, {source,"."}]}] © 2011 Erlang Solutions Ltd.
  • 8. Add Functions 3> meck:expect(test, foo, fun() -> bar end). ok 4> test:foo(). bar 5> meck:expect(test, baz, 0, qux). ok 6> test:baz(). qux © 2011 Erlang Solutions Ltd.
  • 9. Returning Changing Results N EW 7> meck:sequence(test, read, 0, ["foo", "bar", eof]). ok 8> test:read(). "foo" 9> test:read(). "bar" 10> test:read(). eof 11> test:read(). eof © 2011 Erlang Solutions Ltd.
  • 10. Returning Changing Results N EW 12> meck:loop(test, count, 0, [1, 2, 3, 2]). ok 13> [test:count() || _ <- lists:seq(1, 10)]. [1,2,3,2,1,2,3,2,1,2] © 2011 Erlang Solutions Ltd.
  • 11. Delete Functions 14> meck:delete(test, foo, 0). ok 15> test:foo(). ** exception error: undefined function test:foo/0 16> test:module_info(). [{exports,[{read,0},{count,0},{baz,0}, {module_info,0}, {module_info,1}]}, ...] © 2011 Erlang Solutions Ltd.
  • 12. Delete Functions 14> meck:delete(test, foo, 0). ok 15> test:foo(). ** exception error: undefined function test:foo/0 16> test:module_info(). [{exports,[{read,0},{count,0},{baz,0}, {module_info,0}, {module_info,1}]}, ...] © 2011 Erlang Solutions Ltd.
  • 13. Delete Functions 14> meck:delete(test, foo, 0). ok 15> test:foo(). ** exception error: undefined function test:foo/0 16> test:module_info(). [{exports,[{read,0},{count,0},{baz,0}, {module_info,0}, {module_info,1}]}, ...] © 2011 Erlang Solutions Ltd. Image by megapiksel
  • 14. Linking 17> =ERROR REPORT== 30-May-2011::15:10:55 == ** Generic server test_meck terminating ** Last message in was {EXIT,<0.63.0>, {undef,[{test,foo,[]},...]}} ** When Server state == {state,test,...} ** Reason for termination == ** {module could not be loaded, [{test,foo,[]},...]} © 2011 Erlang Solutions Ltd.
  • 15. Linking 18> meck:new(test, [no_link]). ok 19> exit(crash). ** exception exit: crash 20> test:module_info(). [{exports,[{module_info,0}, {module_info,1}]}, ...] © 2011 Erlang Solutions Ltd.
  • 16. Validating a Module 21> meck:validate(test). true 22> Foo = fun(A) when is_integer(A) -> bar end. #Fun<erl_eval.6.80247286> 23> meck:expect(test, foo, Foo). ok 24> test:foo(1). bar 25> meck:validate(test). true © 2011 Erlang Solutions Ltd.
  • 17. Validating a Module 26> test:foo(a). ** exception error: no function clause matching erl_eval:-inside-an-interpreted- fun-(a) 27> meck:validate(test). false © 2011 Erlang Solutions Ltd.
  • 18. Exceptions 28> meck:validate(test). true 29> meck:expect(test, foo, fun() -> exit(crash) end). ok 30> test:foo(). ** exception exit: crash in function meck:exec/4 in call from test:foo/0 called as test:foo() 31> meck:validate(test). false © 2011 Erlang Solutions Ltd.
  • 19. Exceptions 32> meck:expect(test, foo, fun() -> meck:exception(exit, crash) end). ok 33> test:foo(). ** exception exit: crash in function meck:exception/2 in call from test:foo/0 called as test:foo() 34> meck:validate(test). true © 2011 Erlang Solutions Ltd.
  • 20. Exceptions 35> try test:foo() catch _:_ -> erlang:get_stacktrace() end. [{meck,exception,2}, {meck,exec,4}, {test,foo,[]}, {erl_eval,do_apply,5}, {erl_eval,try_clauses,8}, {shell,exprs,7}, {shell,eval_exprs,7}, {shell,eval_loop,3}] © 2011 Erlang Solutions Ltd.
  • 21. Original Modules 36> l(original). {module,original} 37> original:a(). a 38> original:b(). b 6> meck:new(original, [no_link]). ok 7> original:a(). ** exception error: undefined function original:a/0 © 2011 Erlang Solutions Ltd.
  • 22. Original Modules 10> meck:new(original, [no_link, passthrough]). ok 11> meck:expect(original, b, 0, z). ok 12> original:a(). a 13> original:b(). z © 2011 Erlang Solutions Ltd.
  • 23. Original Modules 14> original:double(1). 2 15> meck:expect(original, double, 15> fun(1) -> foo; 15> (A) -> meck:passthrough([A]) 15> end). ok 16> original:double(1). foo 17> original:double(2). 4 © 2011 Erlang Solutions Ltd.
  • 24. History 18> meck:expect(original, a, fun(1) -> foo end). ok 19> original:a(2). ** exception error: function clause... 20> meck:history(original). [{{original,a,[]},a}, % {MFA, R} {{original,b,[]},z}, {{original,c,[1]},foo}, {{original,a,[2]}, % {MFA, C, R, ST} error,function_clause, [{original,a,[2]},...]}] © 2011 Erlang Solutions Ltd.
  • 25. History N EW 21> meck:called(original, a, [2]). true 22> meck:called(original, x, ["nope"]). false Thanks to Susan Potter! © 2011 Erlang Solutions Ltd.
  • 26. Multiple Modules 29> Mods = [x, y, z]. [x,y,z] 30> meck:new(Mods). ok 31> meck:expect(Mods, foo, 0, bar). ok 32> [M:foo() || M <- Mods]. [bar,bar,bar] 33> meck:validate(Mods). true 34> meck:unload(Mods). ok © 2011 Erlang Solutions Ltd.
  • 27. Using meck With EUnit some_test() -> ok = meck:new(t), ?assertEqual(result, m:f()), ?assert(meck:called(t, f, [1, a])), ?assert(meck:validate(t)), ok = meck:unload(t). © 2011 Erlang Solutions Ltd.
  • 28. Using meck With EUnit some_test_() -> {foreach, fun setup/0, fun teardown/1, [fun test1/0, fun test2/0, fun test3/0]}. setup() -> Mods = [x, y, z], meck:new(Mods), meck:expect(x, foo, 0, bar), % ... Mods. teardown(Mods) -> meck:unload(Mods). © 2011 Erlang Solutions Ltd.
  • 29. Behind the Scenes © 2011 Erlang Solutions Ltd.
  • 30. Behind the Scenes test process © 2011 Erlang Solutions Ltd.
  • 31. Behind the Scenes test process mock process © 2011 Erlang Solutions Ltd.
  • 32. Behind the Scenes mocked module test process mock process © 2011 Erlang Solutions Ltd.
  • 33. Behind the Scenes © 2011 Erlang Solutions Ltd.
  • 34. Behind the Scenesmeck:expect(m, f, fun() -> Result end) m:f() ! {get, m, f} ! #Fun<...> execute Result © 2011 Erlang Solutions Ltd.
  • 35. Behind the Scenesmeck:expect(m, f, 0, Result) m:f() execute ! {log, ...} Result © 2011 Erlang Solutions Ltd.
  • 36. Future Developments• Smarter code generation• Better performance• More history validations helpers• Integration with Hamcrest for Erlang - Specific meck helpers © 2011 Erlang Solutions Ltd.
  • 37. Where Is meck Used?• ESL - Many internal projects• Basho - Riak Enterprise• Hibari - The Hibari key value store• Mochi Media - In-game Advertising• 100+ projects on GitHub © 2011 Erlang Solutions Ltd.
  • 38. Get It, Use It, Improve It! github.com/eproxus/meck @eproxus eproxus@gmail.com © 2011 Erlang Solutions Ltd.