Bird
0
0

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

medium📝 Predict Output Q13 of 15
Ruby - Testing with RSpec and Minitest
Given the code below, what will be printed when the test runs?
let(:count) { 0 }
before { count += 1 }
it "increments count" do
  puts count
end
A0
B1
C2
DError: undefined method += for nil
Step-by-Step Solution
Solution:
  1. Step 1: Understand let and before interaction

    let(:count) { 0 } defines a method returning 0. But count is not a variable; it's a method call.
  2. Step 2: Analyze before { count += 1 }

    count += 1 tries to modify count as a variable, but count is a method, so Ruby raises an error: undefined method += for nil or no local variable.
  3. Final Answer:

    Error: undefined method += for nil -> Option D
  4. Quick Check:

    Cannot modify let method return with += [OK]
Quick Trick: let defines methods, not variables; += fails on method calls [OK]
Common Mistakes:
  • Assuming let creates a variable to modify
  • Expecting count to increment automatically
  • Ignoring that let is lazily evaluated method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes