📖 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
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
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
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
Hint
Use puts "Test completed" to print a message after tests run.
Practice
(1/5)
1. What does the assert method in Minitest do?
easy
A. Checks if a condition is true and fails the test if not
B. Runs the entire test suite automatically
C. Prints the test results to the console
D. Defines a new test case class
Solution
Step 1: Understand the purpose of assert
The assert method is used to verify that a condition is true during a test.
Step 2: What happens if the condition is false?
If the condition is false, the test fails and reports an error.
Final Answer:
Checks if a condition is true and fails the test if not -> Option A
Quick Check:
assert checks truth = B [OK]
Hint: Assert checks if condition is true, else test fails [OK]
Common Mistakes:
Thinking assert runs all tests
Confusing assert with printing output
Believing assert defines test classes
2. Which of the following is the correct syntax to write a test method in Minitest using assert style?
easy
A. function test_example() { assert_equal(5, 2 + 3); }
B. def test_example; assert_equal 5, 2 + 3; end
C. test 'example' do; assert_equal 5, 2 + 3; end
D. test_example() { assert_equal 5, 2 + 3 }
Solution
Step 1: Identify Ruby method syntax for Minitest
In Minitest, test methods start with def test_... and end with end.
Step 2: Check the assert usage inside the method
assert_equal expected, actual is used correctly inside the method.
Final Answer:
def test_example; assert_equal 5, 2 + 3; end -> Option B
Quick Check:
Ruby test method syntax = C [OK]
Hint: Test methods start with def test_ and end with end [OK]
Common Mistakes:
Using non-Ruby syntax like function or braces
Using test do blocks which belong to other frameworks
Missing def or end keywords
3. What will be the output when running this Minitest code?
require 'minitest/autorun'
class SampleTest < Minitest::Test
def test_sum
assert_equal 10, 5 + 5
end
def test_fail
assert 3 > 5, '3 is not greater than 5'
end
end
medium
A. One test passes, one test fails with message '3 is not greater than 5'
B. Both tests pass successfully
C. Syntax error due to incorrect assert usage
D. Both tests fail without any message
Solution
Step 1: Analyze test_sum method
assert_equal 10, 5 + 5 checks if 10 equals 10, which is true, so this test passes.
Step 2: Analyze test_fail method
assert 3 > 5 checks if 3 is greater than 5, which is false, so this test fails and shows the message '3 is not greater than 5'.
Final Answer:
One test passes, one test fails with message '3 is not greater than 5' -> Option A
Quick Check:
assert_equal true, assert false with message = D [OK]
Hint: Check each assert condition true or false to predict pass/fail [OK]
Common Mistakes:
Assuming both tests pass
Confusing assert and assert_equal behavior
Ignoring failure message in assert
4. Identify the error in this Minitest code snippet:
class MyTest < Minitest::Test
def test_check
assert_equal(4, 2 + 2
end
end
medium
A. Test method name does not start with 'test_'
B. Incorrect class inheritance from Minitest::Test
C. Missing closing parenthesis in assert_equal call
D. assert_equal arguments are in wrong order
Solution
Step 1: Check syntax of assert_equal call
The line assert_equal(4, 2 + 2 is missing a closing parenthesis.
Step 2: Verify other parts of the code
Class inheritance and method name are correct, and argument order is correct (expected, actual).
Final Answer:
Missing closing parenthesis in assert_equal call -> Option C
Quick Check:
Syntax errors often from missing parentheses = A [OK]
Hint: Count parentheses carefully in assert calls [OK]