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

Getting Started Testing

Getting Started Testing

Concise guide to testing and RSpec basics for new programmers. Gives a good overview of how to test Ruby models with RSpec and steps beginners should take to continue improving their testing skills.

kylefdoherty

May 13, 2015
Tweet

More Decks by kylefdoherty

Other Decks in Programming

Transcript

  1. - I just learned ruby…RSpec WAT? - it’s going to

    slow me way down - how do I know I’m doing it right?
  2. - will help land you a job - good companies

    write tests - easier to refactor code - more thoughtful about solutions - BETTER CODE
  3. class Product attr_reader :name def initialize(name:) @name = name end

    def slug name.downcase.gsub(" ", "-") end end #product.rb
  4. it block describe Product do describe “#slug" do it “slugifies

    the name" do # 4 Phases Go Here end end end
  5. verify it "slugifies the name" do watch = Product.new(name: "Timex

    Weekender") slug = watch.slug expect(slug).to eq("timex-weekender") end
  6. 1. write a test & watch it fail 2. write

    code to make the test pass 3. refactor tests & code
  7. our product class class Product attr_reader :name, :price def initialize(name:,

    price:) @name = name @price = price end def slug name.downcase.gsub(" ", "-") end end
  8. #discount_price - takes the discount % as a parameter (ex:

    0.3) - expect it to return the discounted price - has a default discount of 0.2
  9. describe "#discount_price" do it "returns the correct discounted price" do

    watch = Product.new(name: "Timex Weekender", price: 100) end end #product_spec.rb
  10. describe "#discount_price" do it "returns the correct discounted price" do

    watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) end end #product_spec.rb
  11. describe "#discount_price" do it "returns the correct discounted price" do

    watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end #product_spec.rb
  12. require './game' describe Product do describe "#slug" do it "slugifies

    the name" do watch = Product.new(name: "Timex Weekender", price: 100) slug = watch.slug expect(slug).to eq("timex-weekender") end end describe "#discount_price" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end # takes the discount % as a parameter (ex: 0.3) # expect it to return the discounted price # has a default discount of 0.2 #product_spec.rb
  13. require './game' describe Product do describe "#slug" do it "slugifies

    the name" do watch = Product.new(name: "Timex Weekender", price: 100) slug = watch.slug expect(slug).to eq("timex-weekender") end end describe "#discount_price" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end # takes the discount % as a parameter (ex: 0.3) # expect it to return the discounted price # has a default discount of 0.2 #product_spec.rb
  14. require './game' describe Product do describe "#slug" do it "slugifies

    the name" do watch = Product.new(name: "Timex Weekender", price: 100) slug = watch.slug expect(slug).to eq("timex-weekender") end end describe "#discount_price" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end # takes the discount % as a parameter (ex: 0.3) # expect it to return the discounted price # has a default discount of 0.2 #product_spec.rb
  15. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do end end context "when a discount is given" do it "returns the correct discounted price" do end end end #product_spec.rb
  16. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do end end context "when a discount is given" do it "returns the correct discounted price" do end end end #product_spec.rb
  17. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price expect(discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end #product_spec.rb
  18. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price expect(discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end #product_spec.rb
  19. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price expect(discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end #product_spec.rb
  20. describe "#discount_price" do context "when no discount is given" do

    it "returns the price discounted by the default 20%" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price expect(discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end #product_spec.rb
  21. describe "#slug" do it "slugifies the name" do watch =

    Product.new(name: "Timex Weekender", price: 100) slug = watch.slug expect(slug).to eq("timex-weekender") end end describe "#discount_price" do context "when no discount is given" do it "returns the price discounted 20%" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price expect(discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) discount_price = watch.discount_price(discount: 0.3) expect(discount_price).to eq(70) end end end #product_spec.rb
  22. describe "#slug" do it "slugifies the name" do watch =

    Product.new(name: "Timex Weekender", price: 100) expect(watch.slug).to eq("timex-weekender") end end describe "#discount_price" do context "when no discount is given" do it "returns the price discounted 20%" do watch = Product.new(name: "Timex Weekender", price: 100) expect(watch.discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) expect(watch.discount_price(discount: 0.3)).to eq(70) end end end #product_spec.rb
  23. describe Product do describe "#slug" do it "slugifies the name"

    do watch = Product.new(name: "Timex Weekender", price: 100) expect(watch.slug).to eq("timex-weekender") end end describe "#discount_price" do context "when no discount is given" do it "returns the price discounted 20%" do watch = Product.new(name: "Timex Weekender", price: 100) expect(watch.discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do watch = Product.new(name: "Timex Weekender", price: 100) expect(watch.discount_price(discount: 0.3)).to eq(70) end end end end #product_spec.rb
  24. describe Product do let(:watch) { Product.new(name: "Timex Weekender", price: 100)

    } describe "#slug" do it "slugifies the name" do expect(watch.slug).to eq("timex-weekender") end end describe "#discount_price" do context "when no discount is given" do it "returns the price discounted 20%" do expect(watch.discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do expect(watch.discount_price(discount: 0.3)).to eq(70) end end end end #product_spec.rb
  25. describe Product do let(:watch) { Product.new(name: "Timex Weekender", price: 100)

    } describe "#slug" do it "slugifies the name" do expect(watch.slug).to eq("timex-weekender") end end describe "#discount_price" do context "when no discount is given" do it "returns the price discounted 20%" do expect(watch.discount_price).to eq(80) end end context "when a discount is given" do it "returns the correct discounted price" do expect(watch.discount_price(discount: 0.3)).to eq(70) end end end end #product_spec.rb
  26. tips for success - focus on clarity - you’re telling

    a story - avoid let and other advanced DSL - DRY tests aren’t always the best tests - spike first - binding.pry
  27. next steps - solve 3-5 Project Euler problems - get

    a copy of Everyday Rails Testing with RSpec - chps 2-4 of Rails 4 in Action
  28. recommended resources - Four Phase Test - https://robots.thoughtbot.com/four-phase- test -

    betterspecs.org (let vs. let! & more) - let vs before, https://www.launchacademy.com/codecabulary/ learn-test-driven-development/rspec/before-vs-let - Everyday Rails Testing with RSpec, https://leanpub.com/ everydayrailsrspec - Rails 4 in Action, http://www.amazon.com/Rails-4-Action- Ryan-Bigg/dp/1617291099