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

Managing `let`

Managing `let`

Dive into RSpec's `let`, how it works, and best practices.

Stephanie Viccari

January 04, 2021
Tweet

More Decks by Stephanie Viccari

Other Decks in Education

Transcript

  1. lets recap • defines memoized helper method • is lazy-evaluated

    (unless you let!) • is caches across multiple calls • is not cached across examples 2
  2. lets recap # 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 see the benefits • DRYs up our code *

    • 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
  4. RSpec.describe "User" do let(:org) { FactoryBot.build_stubbed(:organization, name: "AB Inc")} let(:user)

    { 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
  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 14
  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 15
  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 16
  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 17
  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 18
  10. let's not 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 20
  11. lets do 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 21
  12. lets do 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 22
  13. lets do 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 23
  14. lets do 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 24
  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 29
  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 30
  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 31
  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 32
  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 33
  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 34
  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 35
  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 36
  23. Resources 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 37