Mocking and Stubbing in Ruby
📖 Scenario: You are writing tests for a simple Ruby program that fetches weather data from an external service. Since the real service is slow and unreliable for testing, you will use mocking and stubbing to simulate its behavior.
🎯 Goal: Build a test that uses mocking and stubbing to simulate the weather service response and verify your program handles it correctly.
📋 What You'll Learn
Create a class
WeatherService with a method fetch_temperature(city) that normally returns a temperature.Create a test class
WeatherServiceTest using minitest.Stub the
fetch_temperature method to return a fixed value for testing.Mock the
fetch_temperature method call and verify it was called with the correct argument.💡 Why This Matters
🌍 Real World
Mocking and stubbing help you test your code without relying on slow or unreliable external services, making tests faster and more reliable.
💼 Career
Many software development jobs require writing automated tests. Knowing how to mock and stub is essential for testing code that interacts with external systems.
Progress0 / 4 steps