Bird
0
0

Find the error in this RSpec stub code:

medium📝 Debug Q7 of 15
Ruby - Testing with RSpec and Minitest
Find the error in this RSpec stub code:
class Product
  def price
    50
  end
end

product = Product.new
allow(product).to receive(:cost).and_return(100)
puts product.cost
ASyntax error in allow statement
BMethod :cost does not exist on Product
CCannot stub instance methods
DMissing parentheses in method call
Step-by-Step Solution
Solution:
  1. Step 1: Check method existence

    Product class defines price but not cost method.
  2. Step 2: Understand stubbing non-existent methods

    RSpec allows stubbing methods even if they don't exist, but this can cause confusion or errors in strict mode.
  3. Final Answer:

    Method :cost does not exist on Product -> Option B
  4. Quick Check:

    Stubbed method must exist or be handled carefully [OK]
Quick Trick: Stub only existing methods or use doubles [OK]
Common Mistakes:
  • Assuming syntax error in allow
  • Thinking instance methods can't be stubbed
  • Confusing method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes