SlideShare une entreprise Scribd logo
1  sur  58
Testing Gone Right
  from RubyConf 2011 and others
      @jwo - Jesse Wolgamott
What We’ll Talk Abouts
What We’ll Talk Abouts

• Mock is not a 4 letter word
• Fast tests. Really Fast.
• Rails Models
Mock is not a 4 letter
        word
Mocks
Mocks

• Mocks are not stubs
Mocks

• Mocks are not stubs
• Mocks assert Messages
Mocks

• Mocks are not stubs
• Mocks assert Messages
• Assert on state => Stubbing
Mocks

• Mocks are not stubs
• Mocks assert Messages
• Assert on state => Stubbing
• Assert on messages => Mocking
Ruby + OOP + Mock
Ruby + OOP + Mock

• is goood
Ruby + OOP + Mock

• is goood
• Objects should tell objects to do things
Ruby + OOP + Mock

• is goood
• Objects should tell objects to do things
• (Tell don’t ask)
Ruby + OOP + Mock

• is goood
• Objects should tell objects to do things
• (Tell don’t ask)
• You tell to do things using messages
Mock Roles

• Instead of mocking objects, try to mock
  roles
• Wanting to mock concrete objects is a
  design smell
Example
Example

• Restaurant system reserves tables
• Instead of your interface talking to a
  ReservationService:
• Talk to ReservationInterfaceHandler
Message Example
 class ReservationInterface
   def initialize(handler)
     @handler = handler
   end

   def select_table(table)
     @table = table
   end

   def submit_request
     @handler.reserve(@table)
   end
 end
And to Spec it
describe ReservationInterface do

 let(:table){something_interesting_here}
 it "should tell the handler to reserve a table" do
   handler = double('reservation_handler')
   handler.should_receive(:reserve).with(table)
   machine = ReservationInterface.new(handler)
   machine.select_table(table)
   machine.submit_request
 end
Watch Out!
On APIs

• Only Mock Types you own
• Don’t mock boundary objects
• If you do, you’ll duplicate code -> CODE
  SMELL
Other Mock Smells
Other Mock Smells

• Having to setup more than 3 lines -- that’s a
  code smell
Other Mock Smells

• Having to setup more than 3 lines -- that’s a
  code smell
• Mocking concrete objects -- that’s a code
  smell
Other Mock Smells

• Having to setup more than 3 lines -- that’s a
  code smell
• Mocking concrete objects -- that’s a code
  smell
• When tests go brittle and developers
  declare mocks suck -- that’s a code smell
However
However
• If you have a “standard” Rails app,
  programmed procedurally:
However
• If you have a “standard” Rails app,
  programmed procedurally:
• Mocks are not for you
However
• If you have a “standard” Rails app,
  programmed procedurally:
• Mocks are not for you
• If you encapulate your code. If you hide
  information (OOP). If you use messages:
However
• If you have a “standard” Rails app,
  programmed procedurally:
• Mocks are not for you
• If you encapulate your code. If you hide
  information (OOP). If you use messages:
• Mocks are for you!
Mocks Don’t Suck
Mocks Don’t Suck
• If your code is hard to test, you need better
  code
Mocks Don’t Suck
• If your code is hard to test, you need better
  code
• If you have a test that is HARD to setup,
  your class (or test) is doing too much
Mocks Don’t Suck
• If your code is hard to test, you need better
  code
• If you have a test that is HARD to setup,
  your class (or test) is doing too much
• If you see tests with uber-mocks, the
  answer is not to delete mocks. It’s to
  isolate your tests
Fast Rails Tests
Out of the Box
Out of the Box

• Most Rails apps start as CRUD apps
Out of the Box

• Most Rails apps start as CRUD apps
• Tests start fast-ish
Out of the Box

• Most Rails apps start as CRUD apps
• Tests start fast-ish
• Like a frog, we don’t notice the increase
“fast” tests


• Tests should run in under a second
• The entire rspec test suite
How?


• Don’t load rails
Example
             Spec Run
                        Real Time
               Time

With Rails     .07 s      8.1 s


Without
             0.0007 s      .8 s
 Rails
Avoid Factories
Avoid Factories

• Instead, extract your code to classes with
  one single responsibility
Avoid Factories

• Instead, extract your code to classes with
  one single responsibility
• Rather than knowing everything about your
  object
Avoid Factories

• Instead, extract your code to classes with
  one single responsibility
• Rather than knowing everything about your
  object
• Or, load methods via modules
Rails Models
Rails Models

• Are Fat.
Rails Models

• Are Fat.
• Wait, isn’t that good?
Rails Models

• Are Fat.
• Wait, isn’t that good?
• It’s better than controller logic
Rails Models

• Are Fat.
• Wait, isn’t that good?
• It’s better than controller logic
• But no.
What then?
What then?

• Your models can have validations
What then?

• Your models can have validations
• Call out to policy or decision classes for
  business logic
What then?

• Your models can have validations
• Call out to policy or decision classes for
  business logic
• Use Ruby Classes
What then?

• Your models can have validations
• Call out to policy or decision classes for
  business logic
• Use Ruby Classes
• Use Presenters
Example

• Example of building a burrito that is
  delicious, and can know it’s delicious
• In Isolation: https://gist.github.com/1281093
• Using Modules: https://gist.github.com/
  1281064
Credits
Why you don’t get mock objects @gregmoeck

Your tests are lying to you        @chrismdp

Fast Rails talk at gogoruco 2011   @coreyhaines
Testing in Isolation.
                                   @garybernhardt
destroyalldoftware.com

Contenu connexe

Tendances

Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slides
Pat Zearfoss
 

Tendances (10)

Perfect Styling - How to write better CSS
Perfect Styling - How to write better CSSPerfect Styling - How to write better CSS
Perfect Styling - How to write better CSS
 
Dont Try Kotlin on The Backend or You'll Get Hooked
Dont Try Kotlin on The Backend or You'll Get HookedDont Try Kotlin on The Backend or You'll Get Hooked
Dont Try Kotlin on The Backend or You'll Get Hooked
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
QA / Testing Tools, Automation Testing, Online & Classroom Training
QA / Testing Tools, Automation Testing, Online & Classroom Training QA / Testing Tools, Automation Testing, Online & Classroom Training
QA / Testing Tools, Automation Testing, Online & Classroom Training
 
Funtional Ruby - Mikhail Bortnyk
Funtional Ruby - Mikhail BortnykFuntional Ruby - Mikhail Bortnyk
Funtional Ruby - Mikhail Bortnyk
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Herding a Cat with Antlers - Catalyst 5.80
Herding a Cat with Antlers - Catalyst 5.80Herding a Cat with Antlers - Catalyst 5.80
Herding a Cat with Antlers - Catalyst 5.80
 
Frederick web meetup slides
Frederick web meetup slidesFrederick web meetup slides
Frederick web meetup slides
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScript
 
2 the essentials of effective java
2 the essentials of effective java2 the essentials of effective java
2 the essentials of effective java
 

En vedette

The horror story
The horror storyThe horror story
The horror story
ceebee2009
 

En vedette (12)

Emprendimiento yennifer aguirre
Emprendimiento yennifer aguirreEmprendimiento yennifer aguirre
Emprendimiento yennifer aguirre
 
Agile in 90minutes
Agile in 90minutesAgile in 90minutes
Agile in 90minutes
 
The horror story
The horror storyThe horror story
The horror story
 
Planning for Value
Planning for ValuePlanning for Value
Planning for Value
 
Xxxxxx karim a hamir 2015 resume
Xxxxxx karim a  hamir 2015 resumeXxxxxx karim a  hamir 2015 resume
Xxxxxx karim a hamir 2015 resume
 
Land preparation and potato planting
Land preparation and potato plantingLand preparation and potato planting
Land preparation and potato planting
 
Dev ops for devs
Dev ops for devsDev ops for devs
Dev ops for devs
 
Test Driven Design
Test Driven DesignTest Driven Design
Test Driven Design
 
No Projects - Beyond Projects (Refreshed version)
No Projects - Beyond Projects (Refreshed version)No Projects - Beyond Projects (Refreshed version)
No Projects - Beyond Projects (Refreshed version)
 
Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)
Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)
Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)
 
Zero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSSZero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSS
 
Corona effect
Corona effectCorona effect
Corona effect
 

Similaire à Testing gone-right

WTF TDD?
WTF TDD?WTF TDD?
WTF TDD?
jeremyw
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 

Similaire à Testing gone-right (20)

Learn To Test Like A Grumpy Programmer - 3 hour workshop
Learn To Test Like A Grumpy Programmer - 3 hour workshopLearn To Test Like A Grumpy Programmer - 3 hour workshop
Learn To Test Like A Grumpy Programmer - 3 hour workshop
 
Tackling Testing Telephony
Tackling Testing Telephony Tackling Testing Telephony
Tackling Testing Telephony
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Completely Test-Driven
Completely Test-DrivenCompletely Test-Driven
Completely Test-Driven
 
WTF TDD?
WTF TDD?WTF TDD?
WTF TDD?
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Rails traps
Rails trapsRails traps
Rails traps
 
Developer testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to IntegrateDeveloper testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to Integrate
 
Testing with laravel
Testing with laravelTesting with laravel
Testing with laravel
 
Clean code
Clean codeClean code
Clean code
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
What rails taught me – Eugene Pirogov
What rails taught me – Eugene PirogovWhat rails taught me – Eugene Pirogov
What rails taught me – Eugene Pirogov
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Bigger Unit Test Are Better
Bigger Unit Test Are BetterBigger Unit Test Are Better
Bigger Unit Test Are Better
 
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
 
Testing sad-paths
Testing sad-pathsTesting sad-paths
Testing sad-paths
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Dernier (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Testing gone-right

  • 1. Testing Gone Right from RubyConf 2011 and others @jwo - Jesse Wolgamott
  • 3. What We’ll Talk Abouts • Mock is not a 4 letter word • Fast tests. Really Fast. • Rails Models
  • 4. Mock is not a 4 letter word
  • 7. Mocks • Mocks are not stubs • Mocks assert Messages
  • 8. Mocks • Mocks are not stubs • Mocks assert Messages • Assert on state => Stubbing
  • 9. Mocks • Mocks are not stubs • Mocks assert Messages • Assert on state => Stubbing • Assert on messages => Mocking
  • 10. Ruby + OOP + Mock
  • 11. Ruby + OOP + Mock • is goood
  • 12. Ruby + OOP + Mock • is goood • Objects should tell objects to do things
  • 13. Ruby + OOP + Mock • is goood • Objects should tell objects to do things • (Tell don’t ask)
  • 14. Ruby + OOP + Mock • is goood • Objects should tell objects to do things • (Tell don’t ask) • You tell to do things using messages
  • 15. Mock Roles • Instead of mocking objects, try to mock roles • Wanting to mock concrete objects is a design smell
  • 17. Example • Restaurant system reserves tables • Instead of your interface talking to a ReservationService: • Talk to ReservationInterfaceHandler
  • 18. Message Example class ReservationInterface def initialize(handler) @handler = handler end def select_table(table) @table = table end def submit_request @handler.reserve(@table) end end
  • 19. And to Spec it describe ReservationInterface do let(:table){something_interesting_here} it "should tell the handler to reserve a table" do handler = double('reservation_handler') handler.should_receive(:reserve).with(table) machine = ReservationInterface.new(handler) machine.select_table(table) machine.submit_request end
  • 21. On APIs • Only Mock Types you own • Don’t mock boundary objects • If you do, you’ll duplicate code -> CODE SMELL
  • 23. Other Mock Smells • Having to setup more than 3 lines -- that’s a code smell
  • 24. Other Mock Smells • Having to setup more than 3 lines -- that’s a code smell • Mocking concrete objects -- that’s a code smell
  • 25. Other Mock Smells • Having to setup more than 3 lines -- that’s a code smell • Mocking concrete objects -- that’s a code smell • When tests go brittle and developers declare mocks suck -- that’s a code smell
  • 27. However • If you have a “standard” Rails app, programmed procedurally:
  • 28. However • If you have a “standard” Rails app, programmed procedurally: • Mocks are not for you
  • 29. However • If you have a “standard” Rails app, programmed procedurally: • Mocks are not for you • If you encapulate your code. If you hide information (OOP). If you use messages:
  • 30. However • If you have a “standard” Rails app, programmed procedurally: • Mocks are not for you • If you encapulate your code. If you hide information (OOP). If you use messages: • Mocks are for you!
  • 32. Mocks Don’t Suck • If your code is hard to test, you need better code
  • 33. Mocks Don’t Suck • If your code is hard to test, you need better code • If you have a test that is HARD to setup, your class (or test) is doing too much
  • 34. Mocks Don’t Suck • If your code is hard to test, you need better code • If you have a test that is HARD to setup, your class (or test) is doing too much • If you see tests with uber-mocks, the answer is not to delete mocks. It’s to isolate your tests
  • 36. Out of the Box
  • 37. Out of the Box • Most Rails apps start as CRUD apps
  • 38. Out of the Box • Most Rails apps start as CRUD apps • Tests start fast-ish
  • 39. Out of the Box • Most Rails apps start as CRUD apps • Tests start fast-ish • Like a frog, we don’t notice the increase
  • 40. “fast” tests • Tests should run in under a second • The entire rspec test suite
  • 42. Example Spec Run Real Time Time With Rails .07 s 8.1 s Without 0.0007 s .8 s Rails
  • 44. Avoid Factories • Instead, extract your code to classes with one single responsibility
  • 45. Avoid Factories • Instead, extract your code to classes with one single responsibility • Rather than knowing everything about your object
  • 46. Avoid Factories • Instead, extract your code to classes with one single responsibility • Rather than knowing everything about your object • Or, load methods via modules
  • 49. Rails Models • Are Fat. • Wait, isn’t that good?
  • 50. Rails Models • Are Fat. • Wait, isn’t that good? • It’s better than controller logic
  • 51. Rails Models • Are Fat. • Wait, isn’t that good? • It’s better than controller logic • But no.
  • 53. What then? • Your models can have validations
  • 54. What then? • Your models can have validations • Call out to policy or decision classes for business logic
  • 55. What then? • Your models can have validations • Call out to policy or decision classes for business logic • Use Ruby Classes
  • 56. What then? • Your models can have validations • Call out to policy or decision classes for business logic • Use Ruby Classes • Use Presenters
  • 57. Example • Example of building a burrito that is delicious, and can know it’s delicious • In Isolation: https://gist.github.com/1281093 • Using Modules: https://gist.github.com/ 1281064
  • 58. Credits Why you don’t get mock objects @gregmoeck Your tests are lying to you @chrismdp Fast Rails talk at gogoruco 2011 @coreyhaines Testing in Isolation. @garybernhardt destroyalldoftware.com

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n