SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
Mockfight!
          FlexMock vs. Mocha
          Patrick Reagan
          patrick@viget.com
          Director, Application Development




Viget Labs
we build web business



July 18, 2007                 © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      2




What are Mocks?


          “Mock objects are simulated
          objects that mimic the behavior of
          real objects in controlled ways”
                          - Wikipedia


Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      3




When to Mock?

Use mocks when you want to:

• Control the behavior of internal method calls

• Remove dependence on external systems

• Improve test performance



Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      4




Control Behavior

What does this return?




Need to maintain consistency between calls

Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                                      5




Control Behavior

How do we test?


                                                                                   Magic happens
                                                                                   here




Stub the call to Die#rand (using Mocha)


Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      6




Remove Dependencies

Example: retrieve response data via HTTP




To test this, we need to mock:
 • Call to URI#parse
 • Calls to Net::HTTP#new and Net::HTTP#head


Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      7




Mockfight!

Expanded example:
• Create class method to check for resource
• Handle errors and redirects
• Iteratively develop functionality while testing
• Examine the differences between libraries



Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      8




Round 1

Test scenario when URI#parse raises exception




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      9




Round 1: Results

FlexMock




Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      10




Round 2

Check for a valid response




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      11




Round 2: Results

FlexMock




Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      12




Round 3

Check for response type instead of status code




* Our previous tests for response status code are now invalid


Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      13




Round 3: Results

FlexMock




Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      14




Round 4

Follow an HTTP redirect response




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      15




Round 4: Results

FlexMock




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      16




Round 4: Results

Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      17




Round 5

Handling maximum number of redirects




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      18




Round 5: Results

FlexMock




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      19




Round 5: Results

Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      20




Bonus Round

Mocking ActiveRecord#find in controllers




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      21




Bonus Round: Results

FlexMock




Mocha




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      22




But Wait, There’s More

Mocha has parameter matchers (in SVN)
You can write code like this:




Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      23




Conclusion


• Newest versions of gems contain similar features

• FlexMock can accept blocks for further mocking

• Personal preference of syntax is the deciding factor



Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                         24




It’s a Tie!




                                Ultimately, two great libraries - just pick one

Viget Labs
we build web business



July 18, 2007                      © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      25




Questions?




                                                            ???

Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com
Mockfight! FlexMock vs. Mocha                                                      26




Additional Information

FlexMock (Jim Weirich)
          http://www.onestepback.org/software/flexmock


Mocha (James Mead)
          http://mocha.rubyforge.org


                         http://www.slideshare.net/viget
Viget Labs
we build web business



July 18, 2007                   © Copyright 2007 Viget Labs, LLC – www.viget.com

Weitere ähnliche Inhalte

Mehr von Viget Labs

Mehr von Viget Labs (7)

9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did It9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did It
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
Hows Haml?
Hows Haml?Hows Haml?
Hows Haml?
 
Cleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-SpecificityCleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-Specificity
 
Changing Your Mindset: Getting Started With Test-Driven Development
Changing Your Mindset: Getting Started With Test-Driven DevelopmentChanging Your Mindset: Getting Started With Test-Driven Development
Changing Your Mindset: Getting Started With Test-Driven Development
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP Applications
 
Building and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on RailsBuilding and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on Rails
 

Kürzlich hochgeladen

Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
ZurliaSoop
 

Kürzlich hochgeladen (20)

Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentation
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan CytotecJual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
Jual Obat Aborsi ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan Cytotec
 
CROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NSCROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NS
 
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investors
 
New 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck TemplateNew 2024 Cannabis Edibles Investor Pitch Deck Template
New 2024 Cannabis Edibles Investor Pitch Deck Template
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
BeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdfBeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdf
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Rice Manufacturers in India | Shree Krishna Exports
Rice Manufacturers in India | Shree Krishna ExportsRice Manufacturers in India | Shree Krishna Exports
Rice Manufacturers in India | Shree Krishna Exports
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' Slideshare
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 

Mockfight! FlexMock vs. Mocha

  • 1. Mockfight! FlexMock vs. Mocha Patrick Reagan patrick@viget.com Director, Application Development Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 2. Mockfight! FlexMock vs. Mocha 2 What are Mocks? “Mock objects are simulated objects that mimic the behavior of real objects in controlled ways” - Wikipedia Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 3. Mockfight! FlexMock vs. Mocha 3 When to Mock? Use mocks when you want to: • Control the behavior of internal method calls • Remove dependence on external systems • Improve test performance Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 4. Mockfight! FlexMock vs. Mocha 4 Control Behavior What does this return? Need to maintain consistency between calls Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 5. Mockfight! FlexMock vs. Mocha 5 Control Behavior How do we test? Magic happens here Stub the call to Die#rand (using Mocha) Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 6. Mockfight! FlexMock vs. Mocha 6 Remove Dependencies Example: retrieve response data via HTTP To test this, we need to mock: • Call to URI#parse • Calls to Net::HTTP#new and Net::HTTP#head Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 7. Mockfight! FlexMock vs. Mocha 7 Mockfight! Expanded example: • Create class method to check for resource • Handle errors and redirects • Iteratively develop functionality while testing • Examine the differences between libraries Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 8. Mockfight! FlexMock vs. Mocha 8 Round 1 Test scenario when URI#parse raises exception Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 9. Mockfight! FlexMock vs. Mocha 9 Round 1: Results FlexMock Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 10. Mockfight! FlexMock vs. Mocha 10 Round 2 Check for a valid response Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 11. Mockfight! FlexMock vs. Mocha 11 Round 2: Results FlexMock Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 12. Mockfight! FlexMock vs. Mocha 12 Round 3 Check for response type instead of status code * Our previous tests for response status code are now invalid Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 13. Mockfight! FlexMock vs. Mocha 13 Round 3: Results FlexMock Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 14. Mockfight! FlexMock vs. Mocha 14 Round 4 Follow an HTTP redirect response Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 15. Mockfight! FlexMock vs. Mocha 15 Round 4: Results FlexMock Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 16. Mockfight! FlexMock vs. Mocha 16 Round 4: Results Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 17. Mockfight! FlexMock vs. Mocha 17 Round 5 Handling maximum number of redirects Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 18. Mockfight! FlexMock vs. Mocha 18 Round 5: Results FlexMock Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 19. Mockfight! FlexMock vs. Mocha 19 Round 5: Results Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 20. Mockfight! FlexMock vs. Mocha 20 Bonus Round Mocking ActiveRecord#find in controllers Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 21. Mockfight! FlexMock vs. Mocha 21 Bonus Round: Results FlexMock Mocha Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 22. Mockfight! FlexMock vs. Mocha 22 But Wait, There’s More Mocha has parameter matchers (in SVN) You can write code like this: Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 23. Mockfight! FlexMock vs. Mocha 23 Conclusion • Newest versions of gems contain similar features • FlexMock can accept blocks for further mocking • Personal preference of syntax is the deciding factor Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 24. Mockfight! FlexMock vs. Mocha 24 It’s a Tie! Ultimately, two great libraries - just pick one Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 25. Mockfight! FlexMock vs. Mocha 25 Questions? ??? Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com
  • 26. Mockfight! FlexMock vs. Mocha 26 Additional Information FlexMock (Jim Weirich) http://www.onestepback.org/software/flexmock Mocha (James Mead) http://mocha.rubyforge.org http://www.slideshare.net/viget Viget Labs we build web business July 18, 2007 © Copyright 2007 Viget Labs, LLC – www.viget.com