Bird
0
0

Identify the error in this Ruby test double code:

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

Identify the error in this Ruby test double code:

product = double('product')
allow(product).to receive(:price).and_return(100)
puts product.cost
ACalling an unstubbed method <code>cost</code> causes an error
BThe double name should be a symbol, not a string
CThe <code>allow</code> method is used incorrectly
DThe <code>and_return</code> value must be a string
Step-by-Step Solution
Solution:
  1. Step 1: Check stubbed methods

    Only price is stubbed to return 100.
  2. Step 2: Identify method called

    The code calls product.cost, which is not stubbed, causing an error.
  3. Final Answer:

    Calling an unstubbed method cost causes an error -> Option A
  4. Quick Check:

    Unstubbed method call = error [OK]
Quick Trick: Stub all methods you call on doubles to avoid errors [OK]
Common Mistakes:
  • Thinking double names must be symbols
  • Misusing allow syntax
  • Believing return values must be strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes