Bird
0
0

How can you ensure a let variable is re-evaluated before each test when using before hooks that modify it?

hard📝 Application Q9 of 15
Ruby - Testing with RSpec and Minitest
How can you ensure a let variable is re-evaluated before each test when using before hooks that modify it?
AUse <code>let!</code> to force eager evaluation before each test
BReassign the variable inside the before hook
CUse instance variables instead of let
DCall the let variable inside the before hook to reset it
Step-by-Step Solution
Solution:
  1. Step 1: Understand let! behavior

    let! forces the variable to be evaluated eagerly before each test, not lazily.
  2. Step 2: Compare with other options

    Reassigning inside before creates local variables; instance variables are different; calling let inside before does not reset memoization.
  3. Final Answer:

    Use let! to force eager evaluation before each test -> Option A
  4. Quick Check:

    let! = eager evaluation before tests [OK]
Quick Trick: Use let! to evaluate before each test eagerly [OK]
Common Mistakes:
  • Thinking calling let resets memoization
  • Confusing local variable assignment with let
  • Assuming instance variables behave like let

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes