SlideShare ist ein Scribd-Unternehmen logo
1 von 13
ERLYMOCK TUTORIAL
By chaoslawful
What’s ErlyMock?
   A mocking framework for Erlang
   Mock any method in any modules
   Various features:
     Argument   verification
     Call order verification

   Divergence:
     Sheyll’s:  http://erlymock-site.sourceforge.net
     Nialscorva’s:
      https://github.com/nialscorva/erlymock
Looking down from 30,000ft
high
   Sheyll’s version:

    M = em:new(),
    em:strict(M, module, function, [arg, em:any()], {return, ok}),
    em:stub(M, module, function2, [1, 2, 3], {function, fun (_) -> throw(myerror)
    end}),

    % replaying phase
    em:replay(M),

    % test
    ?assertMatches(ok, module:function(arg, anything)),
    ?assertThrow(myerror, module:function2(1, 2, 3)),
    em:verify(M).
Looking down from 30,000ft
high
   Nialscorva version:

    erlymock:start(),
    erlymock:strict(module, function, [arg, '_'], [{return, ok}]),
    erlymock:stub(module, function2, [1, 2, 3], [{throw, myerror}]),

    % initialize
    erlymock:replay(),

    % test
    ?assertMatches(ok, module:function(arg, anything)),
    ?assertThrow(myerror, module:function2(1, 2, 3)),
    erlymock:verify().
ErlyMock(sheyll’s) Funcs
   em:new/0 – Create new mock expectation instance (gen_fsm proc)
   em:strict/4,5 – Strict call expectation
       Strict calls must occur once for each, and in the order of declarations.
       em:strict/4 mocked func always return atom ‘ok’
       em:strict/5 can specify return value with:
           {return, V} – Mocked func return value V
           {function, F} – Mocked func call function F, and use its return value as return value
   em:stub/4,5 – Stub call expectation
       Stub calls can occur any times, and in any order.
       Mocked func return value can be specified as above.
   em:replay/1 – Finish expectation programming phase, start replaying phase
       Must be called before actual test code and after expectation declarataions.
   em:verify/1 – Verify expectations, and destroy mock instance
       Must be called after test code.
   em:any/0 – Return wildcard argument verifier
   em:nothing/2 – Demand no funcs in the specified module can be called
ErlyMock(nialscorva’s) Funcs
   erlymock:start/0 – Start mock service (gen_server proc)
   erlymock:strict/3,4 – Strict call expectation
       Strict calls must occur once for each, and in the order of declarations.
       erlymock:strict/3 mocked funcs always return atom ‘ok’
       erlymock:strict/4 can specify return value with (put in a list):
           {return, V} – Mocked func return value V
           {throw, V} – Mocked func call erlang:throw/1 with reason V
           {exit, V} – Mocked func call erlang:exit/1 with reason V
           {error, V} – Mocked func call erlang:error/1 with reason V
           {function, F} – Mocked func call function F, and use its return value as return value
   erlymock:replay/0 – Finish expectation programming, start replaying phase
       Must be called before actual test code and after expectation declarataions.
   erlymock:verify/0,1 – Verify expectations, and destroy mock instance
       Must be called after test code.
       erlymock:verify/1 can specify timeout for verification process.
ErlyMock(nialscorva’s) Funcs,
cont.
   erlymock:stub/3,4 – Stub call expectation
       Stub calls can occur any times, and in any order.
       Mocked func return value can be specified as above.
       Can retrict minimum and maximum invocation times of
        mocked func with options:
         {min_invocations, Count} – Specify min invocation times,
          default to 0.
         {max_invocations, Count} – Specify max invocation times,
          default to infinity.
   erlymock:o_o/3,4 – Out of order call expectation
       Out of order calls are simply stub calls with
        min_invocation=max_invocation=1, i.e. call exactly once in
        any order.
       Mocked func return value can be specified as above.
Mock example – Missile
Launcher
   Two layer:
       launch_console– Interact with missile operator
       launcher – Called by console, control missile
        hardware
   Need test launch_console:launch/0, it must comply to
    the following restrictions:
     Must   call launcher:confirm/0 first
     If launcher:confirm/0 return false, nothing to do
     If launcher:confirm/0 return true, call launcher:launch/2
      with fixed coordinate
     Time must be retrieved through launcher:time/0
Mock example - Missile
Launcher
        Mocking test cases:
mock_test1() ->
                  M = em:new(),
                  em:stub(M, launcher, time, [], {function, fun () -> Old = case get(mock_time) of undefined -> 0; V -> V end, put(mock_time, Old+1), Old
end}),
                  em:stub(M, launcher, launch, [em:any(), em:any()], {function, fun ()->throw(should_not_happen) end}),
                  em:strict(M, launcher, confirm, [], {return, false}),
                  em:replay(M),
                  launch_console:launch(),
                  em:verify(M).


mock_test2() ->
                  Lat = 33.8, Lon = 45.0,
                  M = em:new(),
                  em:stub(M, launcher, time, [], {function, fun () -> Old = case get(mock_time) of undefined -> 0; V -> V end, put(mock_time, Old+1), Old
end}),
                  em:strict(M, launcher, confirm, [], {return, true}),
                  em:strict(M, launcher, launch, [Lat, Lon]),
                  em:replay(M),
                  launch_console:launch(),
                  em:verify(M).
Commons between impls
   Use beam code hot swapping mechanism to
    implement module mocking.
       See compile:forms/1, code:purge/1, code:delete/1 and
        erlang:load_module/2
       Note:
         Mocking must happened for entire module, restricted by
          this impl method. Mocking partial funcs in a module is
          netiher possible nor desirable.
         Can’t mock the same module in parallel running test cases.

   Mocked funcs are generated on the fly in memory,
    according to declared expectations.
       See erl_syntax module
Sheyll’s compare to
Nialscorva’s
   Pros
       Can recover coverage data after mocking finished
       Shortter module name ;-)
       Support multiple expectation instance (can parallel run test cases if using
        different mock module)
       Implemented in gen_fsm, cleaner than Nialscorva’s
       With ‘nothing’ expectation
       With ‘any’ arg verifier, express wildcard matching in cleaner way
   Cons
       Can’t restrict stub call min/max invocation times
       No out of order call expectation (due to above reason)
       No express way to specify throw/exit/error return value
       No built-in TCP server mocking support (can be done with self-connected socket
        pair)
       No short-cut wildcard arg verifier ‘_’ (but also without its ambiguity)
       Use maven instead of rebar as building tools, not very erly…
Personal thoughts
   Neither impl are good enough…
   For now, recommend Sheyll’s impl prior to
    Nialscorva’s
   We can contribute our efforts to make Sheyll’s
    impl better, it’s not so hard.
     Sheyll’simpl: 641 lines
     Nialscorva’s impl: 843 lines
That’s all, folks!
Q&A

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (18)

Negara Saya
Negara SayaNegara Saya
Negara Saya
 
Themes from Ascent of a Leader
Themes from Ascent of a LeaderThemes from Ascent of a Leader
Themes from Ascent of a Leader
 
General Digital Archive Package Tool
General Digital Archive Package ToolGeneral Digital Archive Package Tool
General Digital Archive Package Tool
 
Wakoo. 9 Septiembre.
Wakoo. 9 Septiembre.Wakoo. 9 Septiembre.
Wakoo. 9 Septiembre.
 
RubyCocoa
RubyCocoaRubyCocoa
RubyCocoa
 
Authority To Refill Existing Prescriptions Mo
Authority To Refill Existing Prescriptions MoAuthority To Refill Existing Prescriptions Mo
Authority To Refill Existing Prescriptions Mo
 
Wg2 Proapganda
Wg2 ProapgandaWg2 Proapganda
Wg2 Proapganda
 
Curriculumlibrary Present
Curriculumlibrary PresentCurriculumlibrary Present
Curriculumlibrary Present
 
Opensocial
OpensocialOpensocial
Opensocial
 
Beekman5 std ppt_16
Beekman5 std ppt_16Beekman5 std ppt_16
Beekman5 std ppt_16
 
Energy multiutility v3
Energy  multiutility v3Energy  multiutility v3
Energy multiutility v3
 
Tle I And Ii Know More About Self
Tle I And Ii   Know More About SelfTle I And Ii   Know More About Self
Tle I And Ii Know More About Self
 
VietRees_Newsletter_25_Tuan1_Thang04
VietRees_Newsletter_25_Tuan1_Thang04VietRees_Newsletter_25_Tuan1_Thang04
VietRees_Newsletter_25_Tuan1_Thang04
 
Pp For Christina
Pp For ChristinaPp For Christina
Pp For Christina
 
6. Social 2.0 Mi Ming Mart And Web 2.0 Erica Yuen
6. Social 2.0   Mi Ming Mart And Web 2.0   Erica Yuen6. Social 2.0   Mi Ming Mart And Web 2.0   Erica Yuen
6. Social 2.0 Mi Ming Mart And Web 2.0 Erica Yuen
 
The Cave
The CaveThe Cave
The Cave
 
Running & Health
Running & HealthRunning & Health
Running & Health
 
VietRees_Newsletter_38_Week1_Month07_Year08
VietRees_Newsletter_38_Week1_Month07_Year08VietRees_Newsletter_38_Week1_Month07_Year08
VietRees_Newsletter_38_Week1_Month07_Year08
 

Mehr von Xiaozhe Wang

C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述Xiaozhe Wang
 
Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析Xiaozhe Wang
 
Erlang抽象数据结构简介
Erlang抽象数据结构简介Erlang抽象数据结构简介
Erlang抽象数据结构简介Xiaozhe Wang
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsXiaozhe Wang
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈Xiaozhe Wang
 
Lua/PHP哈希碰撞攻击浅析
Lua/PHP哈希碰撞攻击浅析Lua/PHP哈希碰撞攻击浅析
Lua/PHP哈希碰撞攻击浅析Xiaozhe Wang
 

Mehr von Xiaozhe Wang (7)

Paxos 简介
Paxos 简介Paxos 简介
Paxos 简介
 
C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述
 
Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析
 
Erlang抽象数据结构简介
Erlang抽象数据结构简介Erlang抽象数据结构简介
Erlang抽象数据结构简介
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈
 
Lua/PHP哈希碰撞攻击浅析
Lua/PHP哈希碰撞攻击浅析Lua/PHP哈希碰撞攻击浅析
Lua/PHP哈希碰撞攻击浅析
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Kürzlich hochgeladen (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Erlymock tutorial

  • 2. What’s ErlyMock?  A mocking framework for Erlang  Mock any method in any modules  Various features:  Argument verification  Call order verification  Divergence:  Sheyll’s: http://erlymock-site.sourceforge.net  Nialscorva’s: https://github.com/nialscorva/erlymock
  • 3. Looking down from 30,000ft high  Sheyll’s version: M = em:new(), em:strict(M, module, function, [arg, em:any()], {return, ok}), em:stub(M, module, function2, [1, 2, 3], {function, fun (_) -> throw(myerror) end}), % replaying phase em:replay(M), % test ?assertMatches(ok, module:function(arg, anything)), ?assertThrow(myerror, module:function2(1, 2, 3)), em:verify(M).
  • 4. Looking down from 30,000ft high  Nialscorva version: erlymock:start(), erlymock:strict(module, function, [arg, '_'], [{return, ok}]), erlymock:stub(module, function2, [1, 2, 3], [{throw, myerror}]), % initialize erlymock:replay(), % test ?assertMatches(ok, module:function(arg, anything)), ?assertThrow(myerror, module:function2(1, 2, 3)), erlymock:verify().
  • 5. ErlyMock(sheyll’s) Funcs  em:new/0 – Create new mock expectation instance (gen_fsm proc)  em:strict/4,5 – Strict call expectation  Strict calls must occur once for each, and in the order of declarations.  em:strict/4 mocked func always return atom ‘ok’  em:strict/5 can specify return value with:  {return, V} – Mocked func return value V  {function, F} – Mocked func call function F, and use its return value as return value  em:stub/4,5 – Stub call expectation  Stub calls can occur any times, and in any order.  Mocked func return value can be specified as above.  em:replay/1 – Finish expectation programming phase, start replaying phase  Must be called before actual test code and after expectation declarataions.  em:verify/1 – Verify expectations, and destroy mock instance  Must be called after test code.  em:any/0 – Return wildcard argument verifier  em:nothing/2 – Demand no funcs in the specified module can be called
  • 6. ErlyMock(nialscorva’s) Funcs  erlymock:start/0 – Start mock service (gen_server proc)  erlymock:strict/3,4 – Strict call expectation  Strict calls must occur once for each, and in the order of declarations.  erlymock:strict/3 mocked funcs always return atom ‘ok’  erlymock:strict/4 can specify return value with (put in a list):  {return, V} – Mocked func return value V  {throw, V} – Mocked func call erlang:throw/1 with reason V  {exit, V} – Mocked func call erlang:exit/1 with reason V  {error, V} – Mocked func call erlang:error/1 with reason V  {function, F} – Mocked func call function F, and use its return value as return value  erlymock:replay/0 – Finish expectation programming, start replaying phase  Must be called before actual test code and after expectation declarataions.  erlymock:verify/0,1 – Verify expectations, and destroy mock instance  Must be called after test code.  erlymock:verify/1 can specify timeout for verification process.
  • 7. ErlyMock(nialscorva’s) Funcs, cont.  erlymock:stub/3,4 – Stub call expectation  Stub calls can occur any times, and in any order.  Mocked func return value can be specified as above.  Can retrict minimum and maximum invocation times of mocked func with options:  {min_invocations, Count} – Specify min invocation times, default to 0.  {max_invocations, Count} – Specify max invocation times, default to infinity.  erlymock:o_o/3,4 – Out of order call expectation  Out of order calls are simply stub calls with min_invocation=max_invocation=1, i.e. call exactly once in any order.  Mocked func return value can be specified as above.
  • 8. Mock example – Missile Launcher  Two layer:  launch_console– Interact with missile operator  launcher – Called by console, control missile hardware  Need test launch_console:launch/0, it must comply to the following restrictions:  Must call launcher:confirm/0 first  If launcher:confirm/0 return false, nothing to do  If launcher:confirm/0 return true, call launcher:launch/2 with fixed coordinate  Time must be retrieved through launcher:time/0
  • 9. Mock example - Missile Launcher  Mocking test cases: mock_test1() -> M = em:new(), em:stub(M, launcher, time, [], {function, fun () -> Old = case get(mock_time) of undefined -> 0; V -> V end, put(mock_time, Old+1), Old end}), em:stub(M, launcher, launch, [em:any(), em:any()], {function, fun ()->throw(should_not_happen) end}), em:strict(M, launcher, confirm, [], {return, false}), em:replay(M), launch_console:launch(), em:verify(M). mock_test2() -> Lat = 33.8, Lon = 45.0, M = em:new(), em:stub(M, launcher, time, [], {function, fun () -> Old = case get(mock_time) of undefined -> 0; V -> V end, put(mock_time, Old+1), Old end}), em:strict(M, launcher, confirm, [], {return, true}), em:strict(M, launcher, launch, [Lat, Lon]), em:replay(M), launch_console:launch(), em:verify(M).
  • 10. Commons between impls  Use beam code hot swapping mechanism to implement module mocking.  See compile:forms/1, code:purge/1, code:delete/1 and erlang:load_module/2  Note:  Mocking must happened for entire module, restricted by this impl method. Mocking partial funcs in a module is netiher possible nor desirable.  Can’t mock the same module in parallel running test cases.  Mocked funcs are generated on the fly in memory, according to declared expectations.  See erl_syntax module
  • 11. Sheyll’s compare to Nialscorva’s  Pros  Can recover coverage data after mocking finished  Shortter module name ;-)  Support multiple expectation instance (can parallel run test cases if using different mock module)  Implemented in gen_fsm, cleaner than Nialscorva’s  With ‘nothing’ expectation  With ‘any’ arg verifier, express wildcard matching in cleaner way  Cons  Can’t restrict stub call min/max invocation times  No out of order call expectation (due to above reason)  No express way to specify throw/exit/error return value  No built-in TCP server mocking support (can be done with self-connected socket pair)  No short-cut wildcard arg verifier ‘_’ (but also without its ambiguity)  Use maven instead of rebar as building tools, not very erly…
  • 12. Personal thoughts  Neither impl are good enough…  For now, recommend Sheyll’s impl prior to Nialscorva’s  We can contribute our efforts to make Sheyll’s impl better, it’s not so hard.  Sheyll’simpl: 641 lines  Nialscorva’s impl: 843 lines