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

Test Doubles

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

Test Doubles

Avatar for Stephanie Viccari

Stephanie Viccari

December 15, 2020
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. U in a ou le RSpec.describe "Book" do describe "#title"

    do it "stands in for a real object" do book_double = double("book", title: "Whale Sounds, Volume I") book_double.title expect(book_double.title).to eq("Whale Sounds, Volume I") end end end 3
  2. C ll ng n nk ow m th d RSpec.describe

    “Book” do describe “#title” do it “stands in for a real object” do book_double = double(“book”, title: “Whale Sounds, Volume I”) book_double.method_that_does_not_exist # => <Double “book’> received unexpected message :method_that_does_not_exist end end end 4
  3. I st nc d ub es class Book def title

    end end RSpec.describe “Book” do describe “#title” do it “stands in for a real object” do invalid_book_double = instance_double("Book", description: "") # => the Book class does not implement the instance method: description end end end 5
  4. L t's om ar double 4 c o a s

    p a o 4 c d n m h s o 4 e : double("book", title: "") 6
  5. L t's om ar i st nc _d ub e

    4 e h d n m m h a o u s 4 e : instance_double(Book, title: "") 4 e : instance_double("Book", title: "") 7
  6. U in a es D ub e o oc s

    ur es class Book attr_reader :title def initialize(title:) @title = title end end RSpec.describe BestSellerTitle do describe "#title" do it "prepends title with 'best seller'" do books = [Book.new(title: "Whale Songs")] title = BestSellerTitle.new.title(book) expect(title).to eq("Best Seller: Whale Songs") end end end 1
  7. U in a es D ub e o oc s

    ur es RSpec.describe BestSellerTitle do describe "#title" do it "prepends title with 'best seller'" do book_double = instance_double(Book, title: "Whale Songs") title = BestSellerTitle.new.title(book_double) expect(title).to eq("Best Seller: Whale Songs") end end end 1
  8. T st ng b ok c nt ol er RSpec.describe

    BooksController do describe "#index" do it "returns 10 published books" do books = FactoryBot.create_list(:book, 10, published: true ) FactoryBot.create(:book, published: false) get :index expect(assigns[:books]).to match_array(books) end end end 1
  9. 1

  10. S ub in o r oo c as RSpec.describe BooksController

    do describe "#index" do it "returns 10 published books" do books = FactoryBot.build_list(:book, 2) allow(Book).to receive(:latest_published).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 1
  11. RSpec.describe BooksController do describe "#index" do it "returns 10 published

    books" do books = FactoryBot.build_list(:book, 2) allow(Book).to receive(:latest_published).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 1
  12. RSpec.describe BooksController do describe "#index" do it "returns 10 published

    books" do books = FactoryBot.build_list(:book, 2) allow(Book).to receive(:latest_published).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 1
  13. RSpec.describe BooksController do describe "#index" do it "returns 10 published

    books" do books = FactoryBot.build_list(:book, 2) allow(Book).to receive(:latest_published).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 1
  14. RSpec.describe BooksController do describe "#index" do it "returns 10 published

    books" do books = FactoryBot.build_list(:book, 2) allow(Book).to receive(:latest_published).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 2
  15. 2

  16. T st ng rg me ts # books_controller.rb Book.limit(per_page) #

    books_controller_spec.rb RSpec.describe BooksController do describe "#index" do it "finds the 10 latest published books" do books = FactoryBot.build_list(:book, 10) allow(Book).to receive(:limit).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 2
  17. T st ng rg me ts # books_controller.rb Book.limit(per_page) #

    books_controller_spec.rb RSpec.describe BooksController do describe "#index" do it "finds the 10 latest published books" do books = FactoryBot.build_list(:book, 10) allow(Book).to receive(:limit).with(10).and_return(books) get :index expect(assigns[:books]).to eq(books) end end end 2
  18. Q er m th ds A k q es io

    4 W h u 's n ? 4 array.empty? 4 string.length 4 user.email 2
  19. C mm nd et od H ve id e e

    ts 4 U h u 's e ! 4 user.save! 4 array.push 4 book.update_attributes 2
  20. S ub in q er m th ds describe Signup

    do context "email" do it "delegates to the user" do user_double = instance_double("User", email: "[email protected]") signup = Signup.new(user: user_double) expect(signup.email).to eq(user.email) end end end 2
  21. S ub in c mm nd et od describe Signup

    do context "#complete" do it "verifies the user's account" do user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  22. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  23. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") expect(user).to receive(:verify).with(email: "[email protected]") signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  24. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") expect(user).to receive(:verify).with(email: "[email protected]") signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  25. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") expect(user).to receive(:verify).with(email: "[email protected]") signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  26. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") expect(user).to receive(:verify).with(email: "[email protected]") signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  27. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do user = instance_double("User") expect(user).to receive(:verify).with(email: "[email protected]") signup = Signup.new(user: user) signup.complete(email: "[email protected]") end end end 3
  28. O de p ea e! describe Signup do context "#complete"

    do it "calls verify on the user" do # Setup user = instance_double("User") signup = Signup.new(user: user) # Setup and also verification??? expect(user).to receive(:verify).with(email: [email protected]) # Exercise signup.complete(email: "[email protected]") end end end 3
  29. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do # Setup user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) # Exercise signup.complete(email: "[email protected]") # Verify expect(user).to have_received(:verify).with(email: [email protected]) end end end 3
  30. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do # Setup user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) # Exercise signup.complete(email: "[email protected]") # Verify expect(user).to have_received(:verify).with(email: [email protected]) end end end 3
  31. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do # Setup user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) # Exercise signup.complete(email: "[email protected]") # Verify expect(user).to have_received(:verify).with(email: [email protected]) end end end 4
  32. S ub in c mm nd et od describe Signup

    do context "#complete" do it "calls verify on the user" do # Setup user = instance_double("User") allow(user).to receive(:verify) signup = Signup.new(user: user) # Exercise signup.complete(email: "[email protected]") # Verify expect(user).to have_received(:verify).with(email: [email protected]) end end end 4