Bird
0
0

Which is the correct syntax to stub a method greet on an object user using RSpec?

easy📝 Syntax Q3 of 15
Ruby - Testing with RSpec and Minitest
Which is the correct syntax to stub a method greet on an object user using RSpec?
Auser.stub(:greet).returns('Hello')
Bstub(user).greet('Hello')
Callow(user).to receive(:greet).and_return('Hello')
Dmock(user).greet = 'Hello'
Step-by-Step Solution
Solution:
  1. Step 1: Recall RSpec stubbing syntax

    RSpec uses allow(object).to receive(:method).and_return(value) to stub.
  2. Step 2: Match syntax to options

    Only allow(user).to receive(:greet).and_return('Hello') matches this correct syntax.
  3. Final Answer:

    allow(user).to receive(:greet).and_return('Hello') -> Option C
  4. Quick Check:

    RSpec stub syntax = allow...receive...and_return [OK]
Quick Trick: Use allow(obj).to receive(:method).and_return(value) [OK]
Common Mistakes:
  • Using deprecated stub syntax
  • Assigning stub value directly
  • Confusing mock and stub syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes