0
0
Rubyprogramming~15 mins

Test doubles concept in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Test doubles concept
📖 Scenario: You are writing tests for a simple Ruby class that sends messages. Sometimes, you want to test your code without actually sending messages. To do this, you use a test double, which acts like a fake message sender.
🎯 Goal: Learn how to create and use a test double in Ruby to simulate a message sender in tests.
📋 What You'll Learn
Create a test double named fake_sender
Set up the test double to respond to send_message method
Call send_message on the test double with a specific message
Print the result of calling send_message on the test double
💡 Why This Matters
🌍 Real World
Test doubles help developers test parts of their code without relying on real objects, which might be slow, unavailable, or have side effects.
💼 Career
Understanding test doubles is important for writing effective automated tests in software development jobs, improving code quality and reliability.
Progress0 / 4 steps
1
Create a test double named fake_sender
Write a line of Ruby code to create a test double called fake_sender using double.
Ruby
Need a hint?

Use double('Name') to create a test double in Ruby.

2
Set up fake_sender to respond to send_message
Write a line of Ruby code to make fake_sender respond to send_message with the string 'Message sent'.
Ruby
Need a hint?

Use allow(double).to receive(:method).and_return(value) to set the response of a test double method.

3
Call send_message on fake_sender with a message
Write a line of Ruby code to call send_message on fake_sender with the argument 'Hello!' and save the result in a variable called result.
Ruby
Need a hint?

Call the method on the test double and assign the return value to result.

4
Print the result of calling send_message
Write a line of Ruby code to print the value of the variable result.
Ruby
Need a hint?

Use puts result to print the value.