RSpec Expectations and Matchers
📖 Scenario: You are writing tests for a simple Ruby method that calculates the square of a number. Testing helps you check if your code works as expected before using it in real projects.
🎯 Goal: Build a basic RSpec test suite using expectations and matchers to verify that a square method returns correct results for given inputs.
📋 What You'll Learn
Create a Ruby method called
square that returns the square of a numberWrite an RSpec test file with a describe block for the
square methodUse
expect with the eq matcher to check the output for input 3Add a test using the
be_a matcher to confirm the output is an IntegerUse the
not_to matcher to verify the output is not zero for input 5💡 Why This Matters
🌍 Real World
Testing code with RSpec is common in Ruby projects to catch bugs early and ensure code quality.
💼 Career
Many Ruby developer jobs require writing and understanding RSpec tests to maintain reliable software.
Progress0 / 4 steps