Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q5 of 15
Ruby - Testing with RSpec and Minitest
What will be the output of the following code?
let(:number) { rand(10) }
before { @num = number }
it 'checks number' do
  expect(@num).to eq(number)
end
ATest fails because @num is nil
BTest passes because @num equals number
CTest fails because number changes between calls
DTest passes because rand(10) returns fixed value
Step-by-Step Solution
Solution:
  1. Step 1: Understand let memoization

    let(:number) calls rand(10) once per example and memoizes the value.
  2. Step 2: Analyze before hook

    @num is assigned the memoized number value.
  3. Step 3: Check expectation

    Both @num and number refer to the same memoized value, so test passes.
  4. Final Answer:

    Test passes because @num equals number -> Option B
  5. Quick Check:

    let memoizes value per example [OK]
Quick Trick: let value stays same within one test example [OK]
Common Mistakes:
  • Thinking rand(10) runs twice per example
  • Assuming number changes between calls
  • Confusing let with before variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes