Bird
0
0

Given the code below, what will be the output when the test runs?

medium📝 Predict Output Q4 of 15
Ruby - Testing with RSpec and Minitest
Given the code below, what will be the output when the test runs?
let(:count) { 0 }
before { @count = count + 1 }
it 'increments count' do
  expect(@count).to eq(1)
end
ATest passes because @count equals 1
BTest fails because @count is nil
CTest fails because count is not defined
DTest fails because @count equals 0
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate let(:count)

    count returns 0 when called.
  2. Step 2: Analyze before hook

    @count is set to count + 1, which is 0 + 1 = 1.
  3. Step 3: Check test expectation

    The test expects @count to equal 1, which matches the value set.
  4. Final Answer:

    Test passes because @count equals 1 -> Option A
  5. Quick Check:

    before sets @count = count + 1 = 1 [OK]
Quick Trick: let is lazy but before runs before test [OK]
Common Mistakes:
  • Assuming let runs before before hook
  • Expecting @count to be nil
  • Confusing instance variable with let variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes