Bird
0
0

How can you verify that a test double cart received the method add_item with argument 'apple' exactly once?

hard📝 Application Q9 of 15
Ruby - Testing with RSpec and Minitest

How can you verify that a test double cart received the method add_item with argument 'apple' exactly once?

Aexpect(cart).to have_received(:add_item).with('apple').once
Ballow(cart).to receive(:add_item).with('apple').once
Ccart.should_receive(:add_item).with('apple').once
Dexpect(cart).to receive(:add_item).with('apple').once
Step-by-Step Solution
Solution:
  1. Step 1: Understand verification vs stubbing

    expect(...).to receive sets expectation before the method call.
  2. Step 2: Identify correct syntax for verifying call

    expect(cart).to receive(:add_item).with('apple').once correctly sets expectation with receive and once. expect(cart).to have_received(:add_item).with('apple').once checks after call but requires have_received only after call. allow(cart).to receive(:add_item).with('apple').once uses allow which stubs but does not verify. cart.should_receive(:add_item).with('apple').once uses old syntax.
  3. Final Answer:

    expect(cart).to receive(:add_item).with('apple').once -> Option D
  4. Quick Check:

    Use expect(...).to receive(...) to verify calls [OK]
Quick Trick: Use expect(...).to receive(...) to verify method calls [OK]
Common Mistakes:
  • Confusing allow with expect for verification
  • Using have_received before method call
  • Using deprecated syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes