name: "Josh") } it "does not instantiate immediately" do # user has not been created yet expect(true).to be_true end it "memoizes the value" do user.update(name: "Stephanie") expect(user.name).to eq("Stephanie") expect(user.name).to eq("Stephanie") end it "does not cache across examples" do expect(user.name).to eq("Josh") end end 3
• Avoids building extra data * • Makes tests easier to read * RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 366.days.ago)} describe "#age" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do it "displays the month and day" do expect(user.birthday).to eq("October 28") end end end 4
{ FactoryBot.build_stubbed(:user, role: "CEO", birthdate: 366.days.ago, organization: org)} describe "#age" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do it "displays the month and day" do expect(user.birthday).to eq("October 28") end end describe "#title" do it "returns the title and company" do expect(user.title).to eq("CEO at AB Inc") end end end 9
"uses the top scoped value" do expect(favorite_number).to eq(5) end describe "within a more specific scope" do let(:favorite_number) { 12 } it "uses the more specific value" do expect(favorite_number).to eq(12) end context "when the number is 3" do let(:favorite_number) { 3 } it "returns 3" do expect(favorite_number).to eq(3) end end end it "uses relative scope" do expect(favorite_number).to eq(5) end end 14
"uses the top scoped value" do expect(favorite_number).to eq(5) end describe "within a more specific scope" do let(:favorite_number) { 12 } it "uses the more specific value" do expect(favorite_number).to eq(12) end context "when the number is 3" do let(:favorite_number) { 3 } it "returns 3" do expect(favorite_number).to eq(3) end end end it "uses relative scope" do expect(favorite_number).to eq(5) end end 15
"uses the top scoped value" do expect(favorite_number).to eq(5) end describe "within a more specific scope" do let(:favorite_number) { 12 } it "uses the more specific value" do expect(favorite_number).to eq(12) end context "when the number is 3" do let(:favorite_number) { 3 } it "returns 3" do expect(favorite_number).to eq(3) end end end it "uses relative scope" do expect(favorite_number).to eq(5) end end 16
"uses the top scoped value" do expect(favorite_number).to eq(5) end describe "within a more specific scope" do let(:favorite_number) { 12 } it "uses the more specific value" do expect(favorite_number).to eq(12) end context "when the number is 3" do let(:favorite_number) { 3 } it "returns 3" do expect(favorite_number).to eq(3) end end end it "uses relative scope" do expect(favorite_number).to eq(5) end end 17
"uses the top scoped value" do expect(favorite_number).to eq(5) end describe "within a more specific scope" do let(:favorite_number) { 12 } it "uses the more specific value" do expect(favorite_number).to eq(12) end context "when the number is 3" do let(:favorite_number) { 3 } it "returns 3" do expect(favorite_number).to eq(3) end end end it "uses relative scope" do expect(favorite_number).to eq(5) end end 18
age given birthdate" do user = FactoryBot.build_stubbed(:user, birthdate: 366.days.ago) expect(user.age).to eq(1) end end describe "#birthday" do it "displays the month and day" do user = FactoryBot.build_stubbed(:user, birthdate: 366.days.ago) expect(user.birthday).to eq("October 28") end end describe "#title" do it "displays the title and company" do org = FactoryBot.build_stubbed(:organization, name: "AB Inc") user = FactoryBot.build_stubbed(:user, role: "CEO", birthdate: 366.days.ago, organization: org) expect(user.title).to eq("CEO at AB Inc") end end end 20
} describe "#age" do let(:birthdate) { 366.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do let(:birthdate) { 366.days.ago } it "displays the month and day" do expect(user.birthday).to eq("October 28") end end describe "#title" do let(:user) { FactoryBot.build_stubbed(:user, role: "CEO", organization: org) } let(:org) { FactoryBot.build_stubbed(:org, name: "AB Inc") } it "displays the title and company" do expect(user.title).to eq("CEO at AB Inc") end end end 21
} describe "#age" do let(:birthdate) { 366.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do let(:birthdate) { 366.days.ago } it "displays the month and day" do expect(user.birthday).to eq("October 28") end end describe "#title" do let(:user) { FactoryBot.build_stubbed(:user, role: "CEO", organization: org) } let(:org) { FactoryBot.build_stubbed(:org, name: "AB Inc") } it "displays the title and company" do expect(user.title).to eq("CEO at AB Inc") end end end 22
} describe "#age" do let(:birthdate) { 366.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do let(:birthdate) { 366.days.ago } it "displays the month and day" do expect(user.birthday).to eq("October 28") end end describe "#title" do let(:user) { FactoryBot.build_stubbed(:user, role: "CEO", organization: org) } let(:org) { FactoryBot.build_stubbed(:org, name: "AB Inc") } it "displays the title and company" do expect(user.title).to eq("CEO at AB Inc") end end end 23
} describe "#age" do let(:birthdate) { 366.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end describe "#birthday" do let(:birthdate) { 366.days.ago } it "displays the month and day" do expect(user.birthday).to eq("October 28") end end describe "#title" do let(:user) { FactoryBot.build_stubbed(:user, role: "CEO", organization: org) } let(:org) { FactoryBot.build_stubbed(:org, name: "AB Inc") } it "displays the title and company" do expect(user.title).to eq("CEO at AB Inc") end end end 24
do context "when the user is exactly one year old" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 385.days.ago)} it "rounds down" do expect(user.age).to eq(1) end end end end 29
do context "when the user is exactly one year old" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 385.days.ago)} it "rounds down" do expect(user.age).to eq(1) end end end end 30
do context "when the user is exactly one year old" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 385.days.ago)} it "rounds down" do expect(user.age).to eq(1) end end end end 31
do context "when the user is exactly one year old" do it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 385.days.ago)} it "rounds down" do expect(user.age).to eq(1) end end end end 32
do context "when the user is exactly one year old" do let(:birthdate) { 365.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:birthdate) { 385.days.ago } it "rounds down" do expect(user.age).to eq(1) end end end end 33
do context "when the user is exactly one year old" do let(:birthdate) { 365.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:birthdate) { 385.days.ago } it "rounds down" do expect(user.age).to eq(1) end end end end 34
do context "when the user is exactly one year old" do let(:birthdate) { 365.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:birthdate) { 385.days.ago } it "rounds down" do expect(user.age).to eq(1) end end end end 35
do context "when the user is exactly one year old" do let(:birthdate) { 365.days.ago } it "calculates age given birthdate" do expect(user.age).to eq(1) end end context "when they are less than six months towards their next year" do let(:birthdate) { 385.days.ago } it "rounds down" do expect(user.age).to eq(1) end end end end 36
https://thoughtbot.com/blog/lets-not Mystery guest - https://thoughtbot.com/blog/mystery-guest The self-contained test - https://thoughtbot.com/blog/the-self- contained-test The case for WET tests - https://thoughtbot.com/blog/the-case-for-wet- tests 37