fake_sendersend_message methodsend_message on the test double with a specific messagesend_message on the test doublefake_sendersend_message methodsend_message on the test double with a specific messagesend_message on the test doublefake_senderfake_sender using double.Use double('Name') to create a test double in Ruby.
fake_sender to respond to send_messagefake_sender respond to send_message with the string 'Message sent'.Use allow(double).to receive(:method).and_return(value) to set the response of a test double method.
send_message on fake_sender with a messagesend_message on fake_sender with the argument 'Hello!' and save the result in a variable called result.Call the method on the test double and assign the return value to result.
send_messageresult.Use puts result to print the value.
What is the main purpose of using test doubles in Ruby testing?
Which of the following is the correct way to create a test double named user in Ruby using RSpec?
?double to create test doubles.double('name'), so double('user') is right.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.nameuser is told to respond to name with 'Alice'.user.name returns 'Alice' as set by and_return.Identify the error in this test double usage and how to fix it:
order = double('order')
allow(order).to receive(:total)
puts order.totaltotal is allowed but no return value is set, so it returns nil.and_return(value) to specify what total should return.and_return causes order.total to return nil, fix by adding and_return -> Option AYou 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?PaymentProcessor takes a gateway object to call charge.charge with the amount and returns true to simulate success.Gateway, isolating the test.gateway that expects charge with amount and returns true -> Option DPass the test and this pattern moves unknown