0
0
Rubyprogramming~30 mins

Minitest basics (assert style) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Minitest basics (assert style)
📖 Scenario: You are learning how to write simple tests in Ruby using Minitest. Testing helps you check if your code works as expected, like checking if a calculator adds numbers correctly.
🎯 Goal: Build a small Ruby test file using Minitest with assert style to check if a method returns the correct sum of two numbers.
📋 What You'll Learn
Create a method called add that takes two numbers and returns their sum
Write a Minitest test class called TestAdd
Inside the test class, write a test method called test_addition
Use assert_equal to check if add(2, 3) returns 5
Run the test and print the result
💡 Why This Matters
🌍 Real World
Testing code helps catch mistakes early, just like checking your homework before submitting it.
💼 Career
Many software jobs require writing tests to ensure code quality and prevent bugs.
Progress0 / 4 steps
1
Create the add method
Write a method called add that takes two parameters a and b and returns their sum using a + b.
Ruby
Need a hint?

Use def to define the method and return the sum with a + b.

2
Set up the Minitest test class
Add require "minitest/autorun" at the top. Then create a test class called TestAdd that inherits from Minitest::Test.
Ruby
Need a hint?

Use require "minitest/autorun" to load Minitest. Define the class with class TestAdd < Minitest::Test.

3
Write the test method with assert_equal
Inside the TestAdd class, write a method called test_addition. Use assert_equal 5, add(2, 3) to check if adding 2 and 3 returns 5.
Ruby
Need a hint?

Define the test method with def test_addition and use assert_equal to compare expected and actual values.

4
Run the test and print the result
Run the Ruby file with Minitest. The test output should show 1 runs, 1 assertions, 0 failures, 0 errors. Add puts "Test completed" after the test class to print confirmation.
Ruby
Need a hint?

Use puts "Test completed" to print a message after tests run.