Bird
Raised Fist0
Rubyprogramming~20 mins

Test doubles concept in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test Doubles Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby test double example?

Consider this Ruby code using RSpec test doubles. What will be printed?

Ruby
require 'rspec/mocks/standalone'

user = double('User')
allow(user).to receive(:name).and_return('Alice')

puts user.name
AAlice
Bnil
Cundefined method `name' for #<Double User>
DError: NoMethodError
Attempts:
2 left
💡 Hint

Test doubles can be told to return specific values for methods.

🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of a test double in Ruby testing?

Choose the best description of why test doubles are used in Ruby tests.

ATo speed up the Ruby interpreter during tests
BTo replace real objects with simplified stand-ins to isolate tests
CTo automatically generate test data for database records
DTo log all method calls during test execution
Attempts:
2 left
💡 Hint

Think about how test doubles help focus tests on one part of the code.

🔧 Debug
advanced
2:00remaining
Why does this Ruby test double code raise an error?

Look at this Ruby code snippet using RSpec doubles. What causes the error?

Ruby
require 'rspec/mocks/standalone'

order = double('Order')

puts order.total
AMissing 'require' statement for RSpec doubles
BThe double 'Order' is not created correctly without a block
CThe 'puts' method cannot print double objects
DNo method stub for 'total' was defined, so calling it raises an error
Attempts:
2 left
💡 Hint

Think about what happens when you call a method on a double without stubbing it.

📝 Syntax
advanced
1:30remaining
Which Ruby code correctly creates a test double with a stubbed method?

Choose the option that correctly creates a test double named 'Car' with a method 'start' returning 'vroom'.

Acar = double('Car').stub(:start).and_return('vroom')
Bcar = double('Car') { def start; 'vroom'; end }
Ccar = double('Car', start: 'vroom')
Dcar = double('Car').allow(:start).to_return('vroom')
Attempts:
2 left
💡 Hint

Look for the correct syntax to stub a method when creating a double.

🚀 Application
expert
2:30remaining
How many methods will this Ruby test double respond to?

Given this Ruby code, how many methods will the user double respond to?

Ruby
require 'rspec/mocks/standalone'

user = double('User')
allow(user).to receive(:name).and_return('Bob')
allow(user).to receive(:email).and_return('bob@example.com')

methods = user.methods.select { |m| [:name, :email].include?(m) }
puts methods.size
A2
B0
CAll methods of Object plus :name and :email
DRaises an error because methods is undefined
Attempts:
2 left
💡 Hint

Consider only the methods explicitly stubbed on the double.

Practice

(1/5)
1.

What is the main purpose of using test doubles in Ruby testing?

easy
A. To permanently change the behavior of classes
B. To speed up the program execution in production
C. To replace real objects with fake ones to isolate tests
D. To automatically generate user interface elements

Solution

  1. Step 1: Understand what test doubles do

    Test doubles are fake objects used in tests to replace real parts of the program.
  2. Step 2: Identify the main goal

    The goal is to isolate the part being tested by removing outside effects from real objects.
  3. Final Answer:

    To replace real objects with fake ones to isolate tests -> Option C
  4. Quick Check:

    Test doubles isolate tests = A [OK]
Hint: Test doubles fake objects to isolate tests quickly [OK]
Common Mistakes:
  • Thinking test doubles speed up production code
  • Believing test doubles permanently change classes
  • Confusing test doubles with UI tools
2.

Which of the following is the correct way to create a test double named user in Ruby using RSpec?

?
easy
A. user = fake('user')
B. user = double('user')
C. user = stub('user')
D. user = mock_object('user')

Solution

  1. Step 1: Recall RSpec syntax for test doubles

    RSpec uses the method double to create test doubles.
  2. Step 2: Match the correct method call

    The correct syntax is double('name'), so double('user') is right.
  3. Final Answer:

    user = double('user') -> Option B
  4. Quick Check:

    RSpec double creation = A [OK]
Hint: Use double('name') to create test doubles in RSpec [OK]
Common Mistakes:
  • Using fake or stub instead of double
  • Confusing mock_object with double
  • Missing quotes around the name
3.

What will be the output of this Ruby code using a test double?

user = double('user')
allow(user).to receive(:name).and_return('Alice')
puts user.name
medium
A. Alice
B. nil
C. user
D. NoMethodError

Solution

  1. Step 1: Understand the double setup

    The double user is told to respond to name with 'Alice'.
  2. Step 2: Check the method call output

    Calling user.name returns 'Alice' as set by and_return.
  3. Final Answer:

    Alice -> Option A
  4. Quick Check:

    Double returns stubbed value = C [OK]
Hint: Stub method with and_return to control output [OK]
Common Mistakes:
  • Expecting the double name instead of stubbed return
  • Thinking it returns nil by default
  • Assuming NoMethodError without stub
4.

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

order = double('order')
allow(order).to receive(:total)
puts order.total
medium
A. Missing and_return causes order.total to return nil, fix by adding and_return
B. Double name should be a symbol, fix by using double(:order)
C. Cannot call allow on a double, fix by using expect
D. Missing puts before order.total

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]
Hint: 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
5.

You want to test a PaymentProcessor class that calls an external Gateway service. How can you use a test double to ensure your test does not call the real Gateway?

class PaymentProcessor
  def initialize(gateway)
    @gateway = gateway
  end

  def pay(amount)
    @gateway.charge(amount)
  end
end

# Test code here?
hard
A. Remove the gateway parameter and hardcode the call inside PaymentProcessor
B. Call the real Gateway class directly in the test
C. Use allow_any_instance_of(Gateway) to stub charge
D. Create a double for gateway that expects charge with amount and returns true

Solution

  1. Step 1: Understand dependency injection

    The PaymentProcessor takes a gateway object to call charge.
  2. Step 2: Use a test double for gateway

    Create a double that expects charge with the amount and returns true to simulate success.
  3. Step 3: Avoid calling real external service

    This prevents the test from calling the real Gateway, isolating the test.
  4. Final Answer:

    Create a double for gateway that expects charge with amount and returns true -> Option D
  5. Quick Check:

    Use doubles to isolate external calls = D [OK]
Hint: Inject doubles to replace external services in tests [OK]
Common Mistakes:
  • Calling real external services in tests
  • Hardcoding dependencies inside classes
  • Using allow_any_instance_of unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard