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

Managing `let`

Managing `let`

Best Practices for RSpec `let`

Links on the "Further Reading" Slide:

My issues with let - https://thoughtbot.com/blog/my-issues-with-let

Let's not - 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

Stephanie Viccari

December 14, 2020
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. lets ec p 4 d n m d h m

    4 i l -e e (u o let!) 4 i c a m e c 4 i o c a e s 2
  2. lets ec p # let_spec.rb RSpec.describe "let" do let(:user) {

    FactoryBot.build_stubbed(:user, 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
  3. lets ee he en t 4 D u d *

    4 A b g e t * 4 M t e a * 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
  4. RSpec.describe "User" do let(:org) { FactoryBot.create(:organization, name: "AB Inc")} let(:user)

    { FactoryBot.create(: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
  5. # scope_spec.rb RSpec.describe "scope" do let(:favorite_number) { 5 } it

    "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 1
  6. # scope_spec.rb RSpec.describe "scope" do let(:favorite_number) { 5 } it

    "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 1
  7. # scope_spec.rb RSpec.describe "scope" do let(:favorite_number) { 5 } it

    "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 1
  8. # scope_spec.rb RSpec.describe "scope" do let(:favorite_number) { 5 } it

    "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 1
  9. # scope_spec.rb RSpec.describe "scope" do let(:favorite_number) { 5 } it

    "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 1
  10. let's ot RSpec.describe "User" do describe "#age" do it "calculates

    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 2
  11. lets o RSpec.describe "User" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)

    } 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 2
  12. lets o RSpec.describe "User" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)

    } 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 2
  13. lets o RSpec.describe "User" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)

    } 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 2
  14. lets o RSpec.describe "User" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)

    } 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 2
  15. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 365.days.ago)} describe "#age"

    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 2
  16. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 365.days.ago)} describe "#age"

    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 3
  17. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 365.days.ago)} describe "#age"

    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 3
  18. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: 365.days.ago)} describe "#age"

    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 3
  19. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)} describe "#age"

    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 3
  20. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)} describe "#age"

    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 3
  21. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)} describe "#age"

    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 3
  22. RSpec.describe "user" do let(:user) { FactoryBot.build_stubbed(:user, birthdate: birthdate)} describe "#age"

    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 3
  23. F rt er ea in M i t l :

    - h ://t b .c /b /m -i -w -l L 's n : - h ://t b .c /b /l -n M g - h ://t b .c /b /m -g T s -c e t : - h ://t b .c /b /t -s -c e -t T s o E t : - h ://t b .c /b /t -c -f -w -t 3