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

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Kürzlich hochgeladen (20)

SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

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