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

Factories via FactoryBot

Factories via FactoryBot

Creating test setup using FactoryBot Factories

Further reading:

Why Factories:
https://thoughtbot.com/blog/why-factories

Factories should be the bare minimum:
https://thoughtbot.com/blog/factories-should-be-the-bare-minimum

Speed up tests by selectively avoiding FactoryBot:
https://thoughtbot.com/blog/speed-up-tests-by-selectively-avoiding-factory-girl

Remove duplication with FactoryBot's traits:
https://thoughtbot.com/blog/remove-duplication-with-factorygirls-traits

Stephanie Viccari

December 15, 2020
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. T I describe "#full_title" do it "combines the title and

    subtitle" do book = Book.new(title: "POODR", subtitle: "An Agile Primer", publication_year: 2012) expect(book.full_title).to eq "POODR: An Agile Primer" end end 4
  2. T I describe "#full_title" do it "combines the title and

    subtitle" do book = Book.new(title: "POODR", subtitle: "An Agile Primer", publication_year: 2012) expect(book.full_title).to eq "POODR: An Agile Primer" end end 5
  3. T o it le nf describe "#full_title" do it "combines

    the title and subtitle" do expect(example_book.full_title).to eq "POODR: An Agile Primer" end end 6
  4. J st ig t describe "#full_title" do it "combines the

    title and subtitle" do book = Book.new(title: "POODR", subtitle: "An Agile Primer") expect(book.full_title).to eq "POODR: An Agile Primer" end end 7
  5. H nd in r qu re e ds class Book

    < ApplicationRecord validates :title, presence: true validates :subtitle, presence: true validates :publication_year, presence: true end describe "#full_title" do it "combines the title and subtitle" do # Invalid book = Book.new(title: "POODR", subtitle: "An Agile Primer") expect(book.full_title).to eq "POODR: An Agile Primer" end end 8
  6. E te t e ac or m th d at

    er 4 M e m g 4 I l t a i e t 4 I t a d 't i e t # books_factory.rb factory :book do title { "default" } subtitle { "default" } publication_year { 2018 } end # book_spec.rb it "has a publication year" do book = FactoryBot.build_stubbed( :book, title: "POODR", subtitle: "An Agile Primer" ) expect(book.publication_year).to eq 2018 end 9
  7. W y ot se ac or es? 4 E d

    o o o 4 C f e v c n e e 4 I a s a b g o d y # books_factory.rb factory :book do title { "default" } subtitle { "default" } publication_year { 2018 } author end factory :author 1
  8. D I ee a ac or ? 4 D I

    e h d ? 4 D I e a t ? 4 D I e a o t s i o o ? I n , I m o e a f . describe "#full_title" do it "combines the title and subtitle" do book = Book.new(title: "POODR", subtitle: "An Agile Primer") expect(book.full_title).to eq "POODR: An Agile Primer" end end 1
  9. W ic c ea io m th d o n

    ed? 4 build 4 create 4 build_stubbed 4 attributes_for # Return an unsaved user instance user = build(:user) # Return a saved user instance user = create(:user) # return a hash of attributes that can be used to build a user instance attributes = attributes_for(:user) # return an object with all defined attributes stubbed out stub = build_stubbed(:user) 1
  10. W ic c ea io m th d o n

    ed? 4 build 4 create 4 build_stubbed 4 attributes_for # Return an unsaved user instance user = build(:user) # Return a saved user instance user = create(:user) # return a hash of attributes that can be used to build a user instance attributes = attributes_for(:user) # return an object with all defined attributes stubbed out stub = build_stubbed(:user) 1
  11. W at nf s ou d i cl de n

    y ac or ? 4 A l p e! 4 M v i 4 U t o e i e 1
  12. W at nf s ou d i cl de n

    y ac or ? factory :author do name { "Chloe Ardelia Wofford" } pen_name { "Toni Morrison" } end after(:create) do |author| create(:book, author: author) end create(:author).pen_name # Toni Morrison create(:author).book.title # The Bluest Eye 1
  13. W at nf s ou d i cl de n

    y ac or ? factory :author do name { "Chloe Ardelia Wofford" } pen_name { "Toni Morrison" } after(:create) do |author| create(:book, author: author) end end create(:author).pen_name # Toni Morrison create(:author).book.title # The Bluest Eye 1
  14. T ai s factory :author do name { "Chloe Ardelia

    Wofford" } trait :with_pen_name do pen_name { "Toni Morrison" } end trait :with_book do after(:create) do |author| create(:book, author: author) end end factory :author_with_pen_name, traits: [:with_pen_name] factory :author_with_book, traits: [:with_book] factory :author_with_pen_name_and_book, traits: [:with_pen_name, :with_book] end create(:author).pen_name # nil create(:author).book # nil create(:author_with_pen_name) # "Toni Morrison" create(:author_with_book).book.title # "The Bluest Eye" 1
  15. T ai s factory :blog_post do title { "I love

    Ruby" } body { "It's an amazing programming language" } unpublished trait :published do published_at { 2.days.ago } end trait :unpublished do published_at nil end end build(:blog_post).published? # false build(:blog_post, :published).published? # true 1
  16. F rt er ea in W F e : -

    h ://t b .c /b /w -f e F e s h r m : - h ://t b .c /b /f e -s -b -t -b -m S t s v a g F B : - h ://t b .c /b /s -u -t -b -s v -a g-f -g R d t t F B 's t : - h ://t b .c /b /r -d t -w -f g -t 2