Test Doubles

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Test Doubles - Presentation Transcript

    1. TEST DOUBLES Mocks, Stubs and such.
    2. TESTING PHASES def test_beatdown setup sox = Team.create(“Red Sox”) exercise yanks = Team.create(“Yankees”) sox.play(yanks) verify assert yanks.get_beatdown? standings.delete_all teardown end
    3. TESTING PHASES def test_beatdown → setup sox = Team.create(“Red Sox”) exercise yanks = Team.create(“Yankees”) sox.play(yanks) verify assert yanks.get_beatdown? standings.delete_all teardown end
    4. TESTING PHASES def test_beatdown setup sox = Team.create(“Red Sox”) yanks = Team.create(“Yankees”) → excercise sox.play(yanks) verify assert yanks.get_beatdown? standings.delete_all teardown end
    5. TESTING PHASES def test_beatdown setup sox = Team.create(“Red Sox”) exercise yanks = Team.create(“Yankees”) sox.play(yanks) → verify assert yanks.get_beatdown? standings.delete_all teardown end
    6. TESTING PHASES def test_beatdown setup sox = Team.create(“Red Sox”) exercise yanks = Team.create(“Yankees”) sox.play(yanks) verify assert yanks.get_beatdown? standings.delete_all → teardown end
    7. WHAT IS A TEST DOUBLE?
    8. WHAT IS A TEST DOUBLE? SUT
    9. WHAT IS A TEST DOUBLE? SUT DOC
    10. WHAT IS A TEST DOUBLE? Indirect Output SUT DOC
    11. WHAT IS A TEST DOUBLE? Indirect Output SUT DOC Indirect Input
    12. WHAT IS A TEST DOUBLE? Indirect Output SUT Test DOC Indirect Input Double
    13. TEST DOUBLE PATTERNS • Dummy Object • Fake Object • Test Stub • Test Spy • Mock Object
    14. DUMMY OBJECT A placeholder that is passed to the SUT and never used def test_with_dummy a = 1 b = 1 dummy = Object.new hypotenuse = MyTrig.pythagorean(a, b, dummy) assert_equal 1.414213562373095, hypotenuse end
    15. FAKE OBJECT An object which replaces the real DOC with an alternate implementation of the same functionality def test_with_fake MyGeo.pi_calculator = FakePie radius = 2 assert_equal 13, MyGeo.circumference(radius).round end class FakePie def pi; 3.14159; end end
    16. TEST STUB An object which replaces the real DOC to control indirect input def test_with_stub Time.now # => Tues Aug 11 08:13:05 -0400 2009 millenium = Time.parse(“2000/1/1 00:00:00 UTC”) Time.stubs(:now).returns(millenium) @redneck = Redneck.create(:last_name => “Federline”) assert_equal millenium, @redneck.created_at end
    17. MOCK OBJECT An object which replaces the real DOC that can verify indirect output with expectations def test_with_mock mock_logger = mock.expects(:warn).with(“Out of range exception”) CrazyRadar.logger = mock_logger CrazyRadar.scan(“Tom Cruise”) end
    18. TEST SPY A more capable Test Stub allowing verification of indirect output def test_with_spy spy_logger = stub(:warn) CrazyRadar.logger = spy_logger CrazyRadar.scan(“Tom Cruise”) assert_received(spy_logger, :warn) {|expect| expect.with(“Out of range exception”) } end
    19. CONSIDER USING A TEST DOUBLE IF: • The DOC doesn't exist yet • The behavior of the DOC cannot be changed/observed • The DOC is too slow • Use of the DOC could cause unwanted side-effects • Visibility into SUT implementation details are required
    20. OVERUSE CAN LEAD TO: • Over specified tests of the SUT's process, not its result • Fragile tests that break when implementation changes • Untested integration • Less time on Twitter while your build runs
    21. DESIGNING FOR DOUBLES • Dependency Injection • Dependency Lookup
    22. DEPENDENCY INJECTION class Wife cattr_accessor :fridge, :beverage def self.beer_me(request=‘Guinness’) beverage = @fridge.find_beer.open end end def test_beer_quality natty = stub(:quality).returns(‘poor’) cooler = expects(:find_beer).returns(natty) Wife.fridge = cooler Wife.beer_me assert_not_equal ‘top notch’, Wife.beverage.quality end
    23. DEPENDENCY LOOKUP class Wife cattr_accessor :beverage def self.beer_me(request=‘Guinness’) beverage = BeerFactory.create(request).open end end def test_beer_quality natty = stub(:quality).returns(‘poor’) BeerFactory.expects(:create).returns(natty) Wife.beer_me assert_not_equal ‘top notch’, Wife.beverage.quality end
    24. RETROFITTING • Test-Specific Subclasses • Test Hooks
    25. TEST-SPECIFIC SUBCLASSES class Wife cattr :supermarket def self.make_breakfast(request=‘Steak & eggs’) ingredients = supermarket.find(request) prepare(ingredients) end end class TestWife < Wife cattr_accessor :supermarket end
    26. TEST HOOKS class Wife if ENV != ‘TEST’ cattr :supermarket else cattr_accessor :supermarket end def self.make_breakfast(request=‘Steak & eggs’) ingredients = supermarket.find(request) prepare(ingredients) end end
    27. MORE xUnit Test Patterns: http://xunitpatterns.com Mocha: http://github.com/floehopper/mocha Ruby Mocking and Stubbing with rr by Josh RR: http://github.com/btakita/rr Nichols: http://technicalpickles.com/posts/ruby-stubbing- RSpec: http://github.com/dchelimsky/rspec and-mocking-with-rr Mocha fork with Test Spies by Joe Ferris: Timecop for testing time-dependent code: http://github.com/jferris/mocha http://github.com/jtrupiano/timecop http://robots.thoughtbot.com/post/159805295/ GoFish: spy-vs-spy http://github.com/gsterndale/GoFish
    SlideShare Zeitgeist 2009

    + gsterndalegsterndale Nominate

    custom

    256 views, 1 favs, 0 embeds more stats

    Test Doubles in Ruby
    Mocks, Stubs, Fakes, Dummies, more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 256
      • 256 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 3
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories