SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
Erlang Solutions Ltd.



Meck
A Mocking Library for Erlang


                        © 2011 Erlang Solutions Ltd.
What Is Mocking?


  adjective /mäk/ 
    1. Not authentic or real, but
       without the intention to deceive




                © 2011 Erlang Solutions Ltd.
What Is Mocking?


                 • Dummy objects
                 • Fake objects
                 • Stubs
                 • Mocks

http://martinfowler.com/articles/mocksArentStubs.html

                    © 2011 Erlang Solutions Ltd.
What Is Mocking?


                 • Dummy objects
                 • Fake objects
                 • Stubs } meck
                 • Mocks

http://martinfowler.com/articles/mocksArentStubs.html

                    © 2011 Erlang Solutions Ltd.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Behind the Scenes




            © 2011 Erlang Solutions Ltd.
Behind the Scenes


 test process




                © 2011 Erlang Solutions Ltd.
Behind the Scenes


 test process                                  mock process




                © 2011 Erlang Solutions Ltd.
Behind the Scenes

                mocked module
 test process                                    mock process




                  © 2011 Erlang Solutions Ltd.
Behind the Scenes




            © 2011 Erlang Solutions Ltd.
Behind the Scenes
meck:expect(m, f, fun() -> Result end)




           m:f()
                                             ! {get, m, f}

                                             ! #Fun<...>

                              execute
            Result
              © 2011 Erlang Solutions Ltd.
Behind the Scenes
meck:expect(m, f, 0, Result)




           m:f()

                              execute
                                             ! {log, ...}
            Result



              © 2011 Erlang Solutions Ltd.
Future Developments
• Smarter code generation
• Better performance
• More history validations helpers
• Integration with Hamcrest for Erlang
 - Specific meck helpers


                  © 2011 Erlang Solutions Ltd.
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.
Get It, Use It, Improve It!

     github.com/eproxus/meck

               @eproxus

           eproxus@gmail.com



              © 2011 Erlang Solutions Ltd.

Más contenido relacionado

La actualidad más candente

Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTPMustafa TURAN
 
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionSQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionJason Myers
 
The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84Mahmoud Samir Fayed
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My PatienceAdam Lowry
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?Carlos Alonso Pérez
 
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNoSuchCon
 
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...OPITZ CONSULTING Deutschland
 
Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsJason Myers
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideStephen Chin
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIOJohn De Goes
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMJason Myers
 
0003 es5 핵심 정리
0003 es5 핵심 정리0003 es5 핵심 정리
0003 es5 핵심 정리욱래 김
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript PerformanceThomas Fuchs
 
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)Brian Vermeer
 
The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30Mahmoud Samir Fayed
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoRemco Wendt
 
Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)Brian Vermeer
 
Python decorators (中文)
Python decorators (中文)Python decorators (中文)
Python decorators (中文)Yiwei Chen
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Thomas Fuchs
 

La actualidad más candente (20)

Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTP
 
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An IntroductionSQLAlchemy Core: An Introduction
SQLAlchemy Core: An Introduction
 
The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.2 book - Part 16 of 84
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My Patience
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?
 
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
 
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...Test-driven JavaScript Development - OPITZ CONSULTING -  Tobias Bosch - Stefa...
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
 
Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic Migrations
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
 
Erlang/OTP in Riak
Erlang/OTP in RiakErlang/OTP in Riak
Erlang/OTP in Riak
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIO
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORM
 
0003 es5 핵심 정리
0003 es5 핵심 정리0003 es5 핵심 정리
0003 es5 핵심 정리
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript Performance
 
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
 
The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.4 book - Part 7 of 30
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in Django
 
Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)
 
Python decorators (中文)
Python decorators (中文)Python decorators (中文)
Python decorators (中文)
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)
 

Similar a Meck at erlang factory, london 2011

Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang FinalSinarShebl
 
Origins of Elixir programming language
Origins of Elixir programming languageOrigins of Elixir programming language
Origins of Elixir programming languagePivorak MeetUp
 
Erlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The UglyErlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The Uglyenriquepazperez
 
The Ring programming language version 1.8 book - Part 32 of 202
The Ring programming language version 1.8 book - Part 32 of 202The Ring programming language version 1.8 book - Part 32 of 202
The Ring programming language version 1.8 book - Part 32 of 202Mahmoud Samir Fayed
 
Let's begin Behavior Driven Development using RSpec
Let's begin Behavior Driven Development using RSpecLet's begin Behavior Driven Development using RSpec
Let's begin Behavior Driven Development using RSpecKenta Murata
 
Practical Migration to Java 7
Practical Migration to Java 7Practical Migration to Java 7
Practical Migration to Java 7Markus Eisele
 
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commits
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commitsdo ruby ao golang: o que aprendi criando um microserviço depois de 5000 commits
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commitsAndreLeoni1
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in ScalaHermann Hueck
 
Elixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitElixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitTobias Pfeiffer
 
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwift
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwiftMeasures for Growth with Firebase Remote Config & Unit Testing Using RxSwift
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwiftFumiya Sakai
 
More than syntax
More than syntaxMore than syntax
More than syntaxWooga
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance EffectsSergey Kuksenko
 
TDD? Sure, but What About My Legacy Code?
TDD? Sure, but What About My Legacy Code?TDD? Sure, but What About My Legacy Code?
TDD? Sure, but What About My Legacy Code?Rob Myers
 
Erlang For Five Nines
Erlang For Five NinesErlang For Five Nines
Erlang For Five NinesBarcamp Cork
 
Ruby to Elixir - what's great and what you might miss
Ruby to Elixir - what's great and what you might missRuby to Elixir - what's great and what you might miss
Ruby to Elixir - what's great and what you might missTobias Pfeiffer
 

Similar a Meck at erlang factory, london 2011 (20)

Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Elixir
ElixirElixir
Elixir
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang Final
 
Origins of Elixir programming language
Origins of Elixir programming languageOrigins of Elixir programming language
Origins of Elixir programming language
 
Elixir @ Paris.rb
Elixir @ Paris.rbElixir @ Paris.rb
Elixir @ Paris.rb
 
Erlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The UglyErlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The Ugly
 
The Ring programming language version 1.8 book - Part 32 of 202
The Ring programming language version 1.8 book - Part 32 of 202The Ring programming language version 1.8 book - Part 32 of 202
The Ring programming language version 1.8 book - Part 32 of 202
 
Dallas Scala Meetup
Dallas Scala MeetupDallas Scala Meetup
Dallas Scala Meetup
 
Let's begin Behavior Driven Development using RSpec
Let's begin Behavior Driven Development using RSpecLet's begin Behavior Driven Development using RSpec
Let's begin Behavior Driven Development using RSpec
 
Practical Migration to Java 7
Practical Migration to Java 7Practical Migration to Java 7
Practical Migration to Java 7
 
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commits
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commitsdo ruby ao golang: o que aprendi criando um microserviço depois de 5000 commits
do ruby ao golang: o que aprendi criando um microserviço depois de 5000 commits
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
Elixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicitElixir & Phoenix - fast, concurrent and explicit
Elixir & Phoenix - fast, concurrent and explicit
 
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwift
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwiftMeasures for Growth with Firebase Remote Config & Unit Testing Using RxSwift
Measures for Growth with Firebase Remote Config & Unit Testing Using RxSwift
 
More than syntax
More than syntaxMore than syntax
More than syntax
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
TDD? Sure, but What About My Legacy Code?
TDD? Sure, but What About My Legacy Code?TDD? Sure, but What About My Legacy Code?
TDD? Sure, but What About My Legacy Code?
 
Fpga 13-task-and-functions
Fpga 13-task-and-functionsFpga 13-task-and-functions
Fpga 13-task-and-functions
 
Erlang For Five Nines
Erlang For Five NinesErlang For Five Nines
Erlang For Five Nines
 
Ruby to Elixir - what's great and what you might miss
Ruby to Elixir - what's great and what you might missRuby to Elixir - what's great and what you might miss
Ruby to Elixir - what's great and what you might miss
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
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...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Meck at erlang factory, london 2011

  • 1. Erlang Solutions Ltd. Meck A 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 • Mocks http://martinfowler.com/articles/mocksArentStubs.html © 2011 Erlang Solutions Ltd.
  • 4. What Is Mocking? • Dummy objects • Fake objects • Stubs } meck • Mocks http://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 Scenes meck:expect(m, f, fun() -> Result end) m:f() ! {get, m, f} ! #Fun<...> execute Result © 2011 Erlang Solutions Ltd.
  • 35. Behind the Scenes meck: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.