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

Take the pain out of your TDD

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Take the pain out of your TDD

My interpretation of TDD for Brighton Ruby Conf 2014.

Andy Pike

July 21, 2014

Transcript

73 slides
  1. describe Account do describe "#register" do context "when params are

    valid" do let(:params) { {:name => "Andy"} } let(:user) { double } before { allow(User).to receive(:new) { user } } it "saves the user" do expect(user).to receive(:save) { true } subject.register(params) end it "returns :saved" do allow(user).to receive(:save) { true } expect(subject.register(params)).to eq(:saved) end end end end
  2. describe Account do describe "#register" do context "when params are

    valid" do let(:params) { {:name => "Andy"} } let(:user) { double } before { allow(User).to receive(:new) { user } } it "saves the user" do expect(user).to receive(:save) { true } subject.register(params) end it "returns :saved" do allow(user).to receive(:save) { true } expect(subject.register(params)).to eq(:saved) end end end end
  3. WHEN REFACTORING OUT CLASSES THEY DO NOT NEED THEIR OWN

    TESTS AS THEY ARE IMPLEMENTATION DETAIL
  4. FIZZBUZZ Print all numbers in a range. Multiples of 3

    print "Fizz" instead of the number. Multiples of 5 print "Buzz". Multiples of both 3 and 5 print "FizzBuzz". git.io/X5jVWA
  5. describe "FizzBuzz game" do it "shows the homepage" do visit

    root_path expect(page).to have_content(/fizzbuzz/i) end end
  6. context "valid input" do it "shows FizzBuzz numbers between a

    given range" do visit root_path fill_in "Min", :with => "1" fill_in "Max", :with => "10" click_on "Generate" expect(page).to have_content( "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz" ) end end
  7. describe FizzBuzz do describe "#build_list" do context "with a range

    of 1..15" do list = %w(1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz) it "returns #{list}" do expect(subject.build_list(1..15)).to eq(list) end end end end
  8. context "with a range of 1..1" do it "returns 1"

    do expect(subject.build_list(1..1)).to eq(["1"]) end end
  9. context "with a range of 3..3" do it "returns Fizz"

    do expect(subject.build_list(3..3)).to eq(["Fizz"]) end end
  10. class FizzBuzz def build_list(range) [].tap do |list| range.each do |n|

    if n % 3 == 0 list << "Fizz" else list << n.to_s end end end end end
  11. context "with a range of 5..5" do it "returns Buzz"

    do expect(subject.build_list(5..5)).to eq(["Buzz"]) end end
  12. class FizzBuzz def build_list(range) [].tap do |list| range.each do |n|

    if n % 3 == 0 list << "Fizz" elsif n % 5 == 0 list << "Buzz" else list << n.to_s end end end end end
  13. context "with a range of 15..15" do it "returns FizzBuzz"

    do expect(subject.build_list(15..15)).to eq(["FizzBuzz"]) end end
  14. class FizzBuzz def build_list(range) [].tap do |list| range.each do |n|

    if n % 3 == 0 && n % 5 == 0 list << "FizzBuzz" elsif n % 3 == 0 list << "Fizz" elsif n % 5 == 0 list << "Buzz" else list << n.to_s end end end end end
  15. class FizzBuzz def build_list(range) [].tap do |list| range.map{ |n| Number.new(n)

    }.each do |n| processors.each do |processor| processor.new(list, n).process end end end end def processors [ FizzBuzzProcessor, FizzProcessor, BuzzProcessor, DefaultProcessor ] end end
  16. describe Account do describe "#register" do context "when params are

    valid" do let(:params) { {:name => "Andy"} } let(:user) { double } before { allow(User).to receive(:new) { user } } it "saves the user" do expect(user).to receive(:save) { true } subject.register(params) end it "returns :saved" do allow(user).to receive(:save) { true } expect(subject.register(params)).to eq(:saved) end end end end
  17. describe Account do describe "#register" do context "when params are

    valid" do let(:params) { {:name => "Andy"} } it "saves the user" do subject.register(params) expect(User.count).to eq(1) end it "returns :saved" do expect(subject.register(params)).to eq(:saved) end end end end
  18. CREDITS ▸ Ian Cooper - TDD, where did it all

    go wrong? vimeo.com/68375232 ▸ Katrina Owen - Therapeutic Refactoring youtu.be/J4dlF0kcThQ ▸ Sandi Metz/Matt Wynne - #POODL Course kickstartacademy.io

More Decks by Andy Pike

Other Decks in Programming