Consider this Ruby code using RSpec test doubles. What will be printed?
require 'rspec/mocks/standalone' user = double('User') allow(user).to receive(:name).and_return('Alice') puts user.name
Test doubles can be told to return specific values for methods.
The double user is told to return 'Alice' when name is called, so puts user.name prints 'Alice'.
Choose the best description of why test doubles are used in Ruby tests.
Think about how test doubles help focus tests on one part of the code.
Test doubles replace real objects so tests can focus on the code being tested without side effects or dependencies.
Look at this Ruby code snippet using RSpec doubles. What causes the error?
require 'rspec/mocks/standalone' order = double('Order') puts order.total
Think about what happens when you call a method on a double without stubbing it.
By default, doubles do not respond to any methods unless explicitly stubbed. Calling total raises a NoMethodError.
Choose the option that correctly creates a test double named 'Car' with a method 'start' returning 'vroom'.
Look for the correct syntax to stub a method when creating a double.
Option C uses the correct syntax to create a double with a stubbed method. Options B, C, and D have syntax errors or invalid method chains.
Given this Ruby code, how many methods will the user double respond to?
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
Consider only the methods explicitly stubbed on the double.
The double responds only to the stubbed methods :name and :email, so the count is 2.