0
0
Rubyprogramming~30 mins

RSpec describe and it blocks in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
RSpec describe and it blocks
📖 Scenario: You are writing tests for a simple Ruby method that adds two numbers. Testing helps you check if your code works as expected before using it in real projects.
🎯 Goal: You will create an RSpec test file using describe and it blocks to organize and run tests for the addition method.
📋 What You'll Learn
Create a describe block for the add method
Inside describe, write an it block describing the expected behavior
Use expect with eq matcher to check the result of add(2, 3)
Print the test result by running the RSpec test file
💡 Why This Matters
🌍 Real World
Writing tests helps catch bugs early and ensures your code works as expected before sharing or deploying it.
💼 Career
RSpec is widely used in Ruby development jobs to write automated tests, improving code quality and reliability.
Progress0 / 4 steps
1
Create the add method
Write a Ruby method called add that takes two parameters a and b and returns their sum.
Ruby
Need a hint?

Define a method with def add(a, b) and return a + b.

2
Add a describe block for the add method
Write an RSpec describe block for the add method. Use describe 'add' to start the block.
Ruby
Need a hint?

Use RSpec.describe 'add' do to start the test block and end to close it.

3
Add an it block with an expectation
Inside the describe 'add' block, write an it block with description 'adds two numbers'. Inside it, use expect(add(2, 3)).to eq(5) to test the method.
Ruby
Need a hint?

Use it 'adds two numbers' do and inside it write expect(add(2, 3)).to eq(5).

4
Run the RSpec test and print the result
Run the RSpec test file and print the output to show the test passed. Use system('rspec filename.rb') replacing filename.rb with your test file name.
Ruby
Need a hint?

Run rspec command on your test file and print the output.