Bird
0
0

Identify the error in this test double usage and how to fix it:

medium📝 Debug Q14 of 15
Ruby - Testing with RSpec and Minitest

Identify the error in this test double usage and how to fix it:

order = double('order')
allow(order).to receive(:total)
puts order.total
AMissing <code>and_return</code> causes <code>order.total</code> to return nil, fix by adding <code>and_return</code>
BDouble name should be a symbol, fix by using <code>double(:order)</code>
CCannot call <code>allow</code> on a double, fix by using <code>expect</code>
DMissing <code>puts</code> before <code>order.total</code>
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the stub setup

    The method total is allowed but no return value is set, so it returns nil.
  2. Step 2: Fix by adding return value

    Add and_return(value) to specify what total should return.
  3. Final Answer:

    Missing and_return causes order.total to return nil, fix by adding and_return -> Option A
  4. Quick Check:

    Stub needs return value = B [OK]
Quick Trick: Always add and_return to stub methods for expected output [OK]
Common Mistakes:
  • Thinking double name must be symbol
  • Confusing allow with expect usage
  • Forgetting to print output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes