• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Testing gone-right
 

Testing gone-right

on

  • 1,474 views

 

Statistics

Views

Total Views
1,474
Views on SlideShare
961
Embed Views
513

Actions

Likes
1
Downloads
0
Comments
0

9 Embeds 513

http://mrjaba.posterous.com 303
http://comal2.dev 188
http://falling-dusk-4290.heroku.com 8
http://feeds.feedburner.com 7
http://www.hanrss.com 3
http://a0.twimg.com 1
http://webcache.googleusercontent.com 1
http://www.codemav.com 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Testing gone-right Testing gone-right Presentation Transcript

  • 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 itdescribe 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 TimeWith Rails .07 s 8.1 sWithout 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
  • CreditsWhy you don’t get mock objects @gregmoeckYour tests are lying to you @chrismdpFast Rails talk at gogoruco 2011 @coreyhainesTesting in Isolation. @garybernhardtdestroyalldoftware.com