Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013

267 views
248 views

Published on

A talk about testing in ruby and ruby on rails I held at the 2nd vienna.rb meetup. See you on http://viennarb.at/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
267
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013

  1. 1. Bag o’ Tricks: Unit Testing Anton ’tony’ Bangratz https://abangratz.github.com/ @tony_xproanton.bangratz@radarservices.com 2012-01-18
  2. 2. Introduction
  3. 3. The Art of Testing
  4. 4. Not (only) Rails
  5. 5. One Method, One PurposeConsider: class TitanClass . . . def doIt(*args) args[:external_sources].each do |source| args[:data_gatherers] do |gatherer| gatherer.doIt(source) end end processed_data = [] current_gatherers.each do |gatherer| processed_data << process_data(gatherer) end reports = {} processed_data.each do |data| reports[data.id] = Report.new(data) end ReportPrinter.print_all(reports) end end
  6. 6. One Method, One PurposeTest: require ’spec_helper’ describe TitanClass do it "should do everything" do source = double(Source) gatherer = double(Gatherer) args = {external_sources: [source], data_gatherers: [gatherer]} processed = double("ProcessedData") subject.should_receive(process_data) .with(gatherer).and_return(processed) report = double(Report) Report.should_receive(:new).with(processed). and_return(report) ReportPrinter.should_receive(:print_all).with([report]) subject.doIt(args) end end
  7. 7. One Method, One PurposeTest: require ’spec_helper’ describe TitanClass do it "should do everything" do source = double(Source) gatherer = double(Gatherer) args = {external_sources: [source], data_gatherers: [gatherer]} processed = double("ProcessedData") subject.should_receive(process_data) .with(gatherer).and_return(processed) # Yikes, a stub on the subject under test! report = double(Report) Report.should_receive(:new).with(processed). and_return(report) # Stubbing ".new" is rarely a good idea ReportPrinter.should_receive(:print_all).with([report]) # Huh. We know that a ReportPrinter prints reports, # but that’s more than we actually should know ... subject.doIt(args) end
  8. 8. One Test, One Assertion
  9. 9. Exampledescribe SmartClass do it "invokes the data gatherer" do data_gatherer = double(DataGatherer) source = double(Source) data_gatherer.should_receive(gather).with(source)) # Our only assertion! subject.data_gatherer = data_gatherer # DI for the win subject.gather_from_source(source) end end...class SmartClass attr_accessor :data_gatherer def gather_from_source(source) data_gatherer_gather(source) endend
  10. 10. Mocking and Stubbing
  11. 11. Testing Modules
  12. 12. Exampleclass TestSmartModule include SmartModuleenddescribe SmartModule do subject { TestSmartModule.new } it "does something" do subject.something.should ... endend
  13. 13. Testing IO
  14. 14. Well, Rails
  15. 15. Anonymous Controllers
  16. 16. Exampledescribe SomeClass, type: ’controller’ do controller(ApplicationControllerOrSubClass) do def index ... # do something end end it "makes something happen" do ... # assert something happens get :index endend
  17. 17. Testing Models?
  18. 18. Another Layer
  19. 19. TDD
  20. 20. Frameworks
  21. 21. MiniTest or RSpec?
  22. 22. That’s all, folks.
  23. 23. Thanks• ¨ Stephan Kamper• Lisa Crispin• Elisabeth Hendricks• Michael Bolton• Avdi Grimm• Steve Klabnik• Jeremy Evans• ¨ Martin Gamsjager• Robert C. Martin (Uncle Bob)• Andreas Kopecky• Jurgen Strobel ¨• Andreas Tiefenthaler• Michael Kohl• Floor Drees• Friends and colleagues that inspired me

×