Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing TDD at work

Implementing TDD at work

A 30-minute talk which introduces some tips for developers to start practicing Test-Driven Development at work.

Tweet

More Decks by Gonzalo Bulnes Guilpain

Other Decks in Programming

Transcript

  1. Implementing TDD at work a case study in a Ruby

    on Rails dev team Gonzalo Bulnes Guilpain May 30, 2013 DynLang Chile Meetup
  2. focus on what's important prefer continuous over disruptive don't do

    more than you can 1 2 3 Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  3. focus on what's important 1 Implementing TDD at work, with

    RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  4. programming languages allow people to communicate focus on what's important

    Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  5. with people focus on what's important Implementing TDD at work,

    with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  6. that's why we adopt agile working methods “Individuals and interactions

    over processes and tools” – The Agile Manifesto focus on what's important Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  7. so, don't focus on the code tonight focus on what's

    important because you'll find easily the RSpec matchers here when you need them https://github.com/rspec/rspec-expectations Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  8. focus on the mindset and why we write some tests

    and ignore others focus on what's important focus on people, the people you work with Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  9. prefer continuous over disruptive 2 we're talking about team management

    here Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  10. Test to make your co-workers happier write specs as documentation

    so they can get introduced painlessly to your code prefer continuous over disruptive $ rspec --format documentation $ echo “--format documentation” > .rspec Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  11. start on your own prefer continuous over disruptive Implementing TDD

    at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  12. let your co-workers decide how and when to start, they'll

    probably start right after you if it's not much extra work prefer continuous over disruptive “Intrinsic motivation relies on autonomy, mastery and purpose.” – Daniel Pink, Drive Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  13. don't be affraid to start more than once prefer continuous

    over disruptive because you skiped bold announcements, you co-workers may not even have noticed you started twice Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  14. then show them, don't tell them prefer continuous over disruptive

    well documented code is worth a thousand good reasons but tell them how progressively you started Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  15. don't do more than you can 3 “Think big, start

    small. [...] For long-term change, experiment immediately.” – Eric Ries, The Lean Startup Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  16. Hypotesis: if they feel happier with your code when it's

    tested, devs will embrace TDD driven by purpose. don't do more than you can Remember: “your co-workers [...] if it's not much extra work” At this point, more extra work than time means no intrinsic motivation at all. Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  17. I use to start with models specs don't do more

    than you can • simple spec examples • easy to maintain • learning included (factories) • extensible (validations, methods) models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  18. # specs/models/video_spec.rb require 'spec_helper' describe Video do # attributes #

    associations # validations # methods end don't do more than you can models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  19. # specs/models/video_spec.rb require 'spec_helper' describe Video do # attributes it

    “has a width” do should respond_to :width end # ... end don't do more than you can models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  20. # specs/models/video_spec.rb require 'spec_helper' describe Video do # validations it

    “has a valid Factory” do FactoryGirl.build(:video).should be_valid end # ... end don't do more than you can # specs/factories/videos.rb FactoryGirl.define do |f| factory :video do end end models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  21. # specs/models/video_spec.rb require 'spec_helper' describe Video do # validations it

    “requires width presence” do FactoryGirl.build(:video, width: '').should_not be_valid end # ... end don't do more than you can # specs/factories/videos.rb FactoryGirl.define do |f| factory :video do width 240 end end models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  22. # specs/factories/videos.rb FactoryGirl.define do |f| factory :video do width 240

    end factory :invalid_video, class: :video do end end don't do more than you can # specs/models/video_spec.rb describe Video do # validations it “has an invalid Factory” do FactoryGirl.build(:invalid_video).should_not be_valid end end models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  23. # specs/models/video_spec.rb require 'spec_helper' describe Video do # associations it

    “belongs to an article” do should belong_to :article end # ... end don't do more than you can # specs/factories/videos.rb FactoryGirl.define do |f| factory :video do article width 240 end # ... end models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  24. gonzalo@cassis:~/dev/blog[add-videos]$ rspec spec/models/video_spec.rb Video has an height has an invalid

    factory requires width presence requires its height to be an integer has a width requires height presence requires its width to be an integer requires file name presence belongs to an article has a valid factory has a file name Finished in 0.07867 seconds 11 examples, 0 failures Randomized with seed 49893 don't do more than you can models Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  25. keep specs simple don't do more than you can models

    as soon as associations get tricky, skip factories Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  26. then establish common conventions with your fellow devs don't do

    more than you can models if you're here, you were right Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  27. Hypotesis: green test suites are addictive, once they started testing,

    devs main drive to TDD is mastery. don't do more than you can Tip: you'll keep moving one step ahead of your co-workers aiming at showing them the TDD way Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  28. routing specs quickly don't do more than you can routing

    Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  29. # specs/routing/reservations_routing_spec.rb require "spec_helper" describe ReservationsController do describe "routing" do

    it "routes to #index" do get("/reservations").should route_to("reservations#index") end end end don't do more than you can routing Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  30. # specs/routing/reservations_routing_spec.rb require "spec_helper" describe ReservationsController do describe "routing" do

    # ... it "routes to #new" do get("/room/1/reservar").should \ route_to("reservations#new", :room_id => "1") end end end don't do more than you can routing Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  31. lead to controllers specs don't do more than you can

    controllers • scaffold is a good draft • DRY sweeties • deeper factories usage Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  32. don't do more than you can controllers require 'spec_helper' describe

    VideosController do # This should return the minimal set of attributes bla bla bla... def valid_attributes { width: 235 } end describe "GET index" do it "assigns all places as @videos" do video = Video.create! valid_attributes get :index, {} assigns(:videos).should eq([video]) end it "renders the administration layout" do get :index response.should render_template 'layouts/administration' end end # ... end Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  33. don't do more than you can controllers require 'spec_helper' describe

    VideosController do def valid_attributes FactoryGirl.attributes_for(:video).stringify_keys! end def invalid_attributes FactoryGirl.attributes_for(:invalid_video).stringify_keys! end # ... end Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  34. don't do more than you can controllers require 'spec_helper' describe

    VideosController do # see https://github.com/thoughtbot/factory_girl FactoryGirl.build_stubbed(:video) # see https://www.relishapp.com/rspec/rspec_mocks Video.any_instance.stub(:save).and_return(false) # ... end Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  35. follow the path you prefer don't do more than you

    can features, views, helpers e.g. I like to have views helpers well documented because it's easy to forget they exist Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  36. gonzalo@cassis:~/dev/blog[add-videos]$ rspec spec/helpers/videos_helper_spec.rb VideosHelper #results_title when params[:category] is not set

    raises RuntimeError when @videos is not set raises RuntimeError when params[:category] and @videos are set returns a title containing the serach results count and params[:year] is set returns a title with the search results count and year information Finished in 0.07537 seconds 4 examples, 0 failures Randomized with seed 21384 don't do more than you can helpers Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  37. do TDD for your team experiments sometimes fail be patient

    1 2 3 conclusion Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  38. Some reading, about BDD by David Chelimsky (the RSpec author)

    The RSpec Book conclusion Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  39. be ready to answer questions conclusion innovation is about people,

    and people usually get questions One last thing: Implementing TDD at work, with RSpec DynLang Chile Meetup, May 2013 G. Bulnes Guilpain
  40. This talk is free. Implementing TDD at work, with RSpec

    DynLang Chile Meetup, May 2013 G. Bulnes Guilpain This document and its sources can be found at https://github.com/gonzalo-bulnes