Giter Club home page Giter Club logo

mock_stub's Introduction

Erlang Mock And Stub

how mock works

when expect is called, 
  1. it saves the existing module to old_code
  2. build the same name module to call epic_mock:proxy_call
  3. save the expectation to reply to the call
  
when the actual module:function is called
  1. search the expectation, then reply the call

When stop is called
  1. it stores back the original modules

expect must has two arity
  1. Function and Argument
     Arguments could be a real argument or general ones, i.e 'Arity2', 'Arity3'
  2. Reply to the call, expectation
     This could be a term, or a function

compile

erlc -o ebin src/*.erl

test

erlc -DTEST +debug_info -o ebin src/*.erl test/*.erl
erl -pa ebin -noshell -s stub test -eval "eunit:test(stub,[verbose])" -s init stop
erl -pa ebin -noshell -s stub test -eval "eunit:test(mock,[verbose])" -s init stop

Here is a simple example, for more information, please email me at [email protected]

Examples

% for a module
{ok, _} = mock:start(fake_module),
ok = mock:expect({fake_module, foo, []}, bar0),
ok = mock:expect({fake_module, foo, [1]}, bar1),
ok = mock:expect({fake_module, foo, 'Arity2'}, fun(A,B) -> A+B end),
ok = mock:expect({fake_module, foo, 'Arity3'}, foobar),
ok = mock:expect({fake_module, foo, [1,2,3,4]}, fun(A,B,C,D) -> A+B+C+D end),

bar0 = fake_module:foo()),
bar1 = fake_module:foo(1)),
5 =    fake_module:foo(2,3)),
foobar =  fake_module:foo(1,2,3)),
10 =  fake_module:foo(1,2,3,4)),
mock:stop(fake_module),

% for a gen_server
{ok, _} = mock:start(fake_server),
ok = mock:expect({fake_server, init, [[]]}, {ok,[]}),
ok = mock:expect({fake_server, handle_call, 'Arity3'}, {reply, bar, []}),
ok = mock:expect({fake_server, handle_cast, 'Arity2'}, {noreply, []}),
ok = mock:expect({fake_server, handle_info, 'Arity2'}, {noreply, []}),
gen_server:start({local, fake_server}, fake_server, [], []),

bar = gen_server:call(fake_server, foo),
ok  = gen_server:cast(fake_server, foo),
foo = fake_server ! foo,
mock:stop(fake_server).

mock_stub's People

Stargazers

Jon Meredith avatar Aeryn Toland avatar

Watchers

opensource@kineticsocial.com avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.