SlideShare a Scribd company logo
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

More Related Content

KEY
Refactoring RIA Unleashed 2011
PDF
Scala Bay Meetup - The state of Scala code style and quality
PDF
Perfect Code
KEY
Attributes, reflection, and dynamic programming
PDF
From Stairway to Heaven onto the Highway to Hell with Xtext
KEY
Xtext Best Practices
PDF
Getting started with scala cats
PDF
Exception Handling: Designing Robust Software in Ruby
Refactoring RIA Unleashed 2011
Scala Bay Meetup - The state of Scala code style and quality
Perfect Code
Attributes, reflection, and dynamic programming
From Stairway to Heaven onto the Highway to Hell with Xtext
Xtext Best Practices
Getting started with scala cats
Exception Handling: Designing Robust Software in Ruby

What's hot (10)

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

Viewers also liked (12)

PPTX
Emprendimiento yennifer aguirre
PDF
Agile in 90minutes
PDF
The horror story
PPTX
Planning for Value
DOCX
Xxxxxx karim a hamir 2015 resume
PDF
Land preparation and potato planting
PPT
PDF
Test Driven Design
PPTX
No Projects - Beyond Projects (Refreshed version)
PDF
Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)
PPTX
Zero to the Cloud with @NetflixOSS
PPTX
Corona effect
Emprendimiento yennifer aguirre
Agile in 90minutes
The horror story
Planning for Value
Xxxxxx karim a hamir 2015 resume
Land preparation and potato planting
Test Driven Design
No Projects - Beyond Projects (Refreshed version)
Prosci Change Management Workshop for Project Managers Info Webinar (30 mins)
Zero to the Cloud with @NetflixOSS
Corona effect
Ad

Similar to Testing gone-right (20)

PDF
Learn To Test Like A Grumpy Programmer - 3 hour workshop
PDF
Tackling Testing Telephony
PPTX
Principled And Clean Coding
PDF
Completely Test-Driven
KEY
WTF TDD?
PDF
Unit Testing Best Practices
PPTX
Design p atterns
KEY
Rails traps
KEY
Developer testing 201: When to Mock and When to Integrate
PPTX
Testing with laravel
PPTX
Clean code
PDF
Jest: Frontend Testing richtig gemacht @WebworkerNRW
PPTX
A Simple Introduction to Prompt Engineering
PPTX
Ruby basics
PDF
What rails taught me – Eugene Pirogov
PDF
The Cowardly Test-o-Phobe's Guide To Testing
PPTX
Test Driven Development on Android (Kotlin Kenya)
ZIP
Meta Programming in Ruby - Code Camp 2010
PDF
Bigger Unit Test Are Better
PDF
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
Learn To Test Like A Grumpy Programmer - 3 hour workshop
Tackling Testing Telephony
Principled And Clean Coding
Completely Test-Driven
WTF TDD?
Unit Testing Best Practices
Design p atterns
Rails traps
Developer testing 201: When to Mock and When to Integrate
Testing with laravel
Clean code
Jest: Frontend Testing richtig gemacht @WebworkerNRW
A Simple Introduction to Prompt Engineering
Ruby basics
What rails taught me – Eugene Pirogov
The Cowardly Test-o-Phobe's Guide To Testing
Test Driven Development on Android (Kotlin Kenya)
Meta Programming in Ruby - Code Camp 2010
Bigger Unit Test Are Better
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Programs and apps: productivity, graphics, security and other tools
TLE Review Electricity (Electricity).pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Mushroom cultivation and it's methods.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Getting Started with Data Integration: FME Form 101
OMC Textile Division Presentation 2021.pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
WOOl fibre morphology and structure.pdf for textiles
Web App vs Mobile App What Should You Build First.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Heart disease approach using modified random forest and particle swarm optimi...
A comparative analysis of optical character recognition models for extracting...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf

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

Editor's Notes