Test-driven development helps you write code that works right from the start. It makes sure your program does what you want by testing it first.
When you want to build a small feature and be sure it works before adding more.
When fixing bugs to make sure the problem is solved and does not come back.
When learning a new programming concept and want to check your understanding.
When working in a team to keep code reliable and avoid breaking others' work.
When improving old code safely by testing changes step-by-step.
Syntax
Ruby
# 1. Write a test that fails# 2. Write just enough code to pass the test# 3. Run tests and see them pass# 4. Refactor code to improve it# 5. Repeat for next feature or fix
The test is written before the actual code.
Tests guide the coding process and catch mistakes early.
Examples
This is a simple test that checks if 2 + 3 equals 5.
This program shows the full test-driven workflow: first the test checks addition, then the Calculator class is created with the add method to pass the test.
Ruby
require'minitest/autorun'class Calculator
def add(a, b)
a + b
endendclass TestCalculator < Minitest::Test
def test_addition
calc = Calculator.new
assert_equal 5, calc.add(2, 3)
endend
OutputSuccess
Important Notes
Always start by writing a test that fails to know what you want your code to do.
Keep tests small and focused on one thing at a time.
Refactor your code after tests pass to keep it clean and simple.
Summary
Write tests before code to guide development.
Run tests often to catch errors early.
Refactor code safely with tests as a safety net.
Practice
(1/5)
1. In Ruby's test-driven development (TDD) workflow, what is the first step you should do?
easy
A. Run all tests to check for errors
B. Write a failing test that defines the desired behavior
C. Refactor the existing code
D. Write the full implementation code
Solution
Step 1: Understand the TDD cycle
The TDD cycle starts by writing a test that fails because the feature is not implemented yet.
Step 2: Identify the first action in TDD
Writing a failing test first guides the development and ensures the code meets requirements.
Final Answer:
Write a failing test that defines the desired behavior -> Option B
Quick Check:
First step in TDD = Write failing test [OK]
Hint: Remember: Test first, code second in TDD [OK]
Common Mistakes:
Starting by writing implementation code before tests
Refactoring before any test is written
Running tests before any code or test exists
2. Which Ruby command runs all tests in a typical TDD workflow using Minitest?
easy
A. rails server
B. rspec test_file.rb
C. ruby test_file.rb
D. bundle install
Solution
Step 1: Identify the test framework command
Minitest tests are run by executing the Ruby test file directly with ruby test_file.rb.
Step 2: Eliminate unrelated commands
rspec is for RSpec tests, rails server starts the server, and bundle install installs gems.
Final Answer:
ruby test_file.rb -> Option C
Quick Check:
Run Minitest with ruby test_file.rb [OK]
Hint: Run tests by executing test files with ruby [OK]
Common Mistakes:
Using rspec command for Minitest tests
Confusing server start with test run
Running bundle install instead of tests
3. Given this Ruby test and code, what will be the test output?
require 'minitest/autorun'
class Calculator
def add(a, b)
a + b
end
end
class TestCalculator < Minitest::Test
def test_add
calc = Calculator.new
assert_equal 5, calc.add(2, 3)
end
end
medium
A. 1 test, 0 failures
B. 1 test, 1 failure
C. SyntaxError
D. No tests run
Solution
Step 1: Analyze the test and method
The add method returns the sum of two numbers. The test expects 5 from adding 2 and 3.
Step 2: Check if test passes
Since 2 + 3 equals 5, the assertion passes and the test succeeds.
Final Answer:
1 test, 0 failures -> Option A
Quick Check:
Correct sum = test passes [OK]
Hint: If method matches test expectation, test passes [OK]
Common Mistakes:
Thinking test fails when it actually passes
Confusing syntax error with test failure
Ignoring the assert_equal expected value
4. What is wrong with this TDD workflow snippet in Ruby?
class TestCalculator < Minitest::Test
def test_multiply
calc = Calculator.new
assert_equal 6, calc.multiply(2, 3)
end
end
class Calculator
def multiply(a, b)
a - b
end
end
medium
A. The test method name is incorrect
B. assert_equal arguments are reversed
C. Calculator class is missing
D. The multiply method subtracts instead of multiplies
Solution
Step 1: Compare method implementation with test expectation
The test expects multiply(2, 3) to return 6, but the method subtracts a - b.
Step 2: Identify the error cause
Subtracting 2 - 3 gives -1, which will fail the test expecting 6.
Final Answer:
The multiply method subtracts instead of multiplies -> Option D
Quick Check:
Method logic must match test expectation [OK]
Hint: Check method logic matches test assertions [OK]
Common Mistakes:
Confusing subtraction with multiplication
Ignoring method implementation details
Assuming test method name affects test result
5. You want to safely improve your Ruby code after passing tests. Which TDD step lets you do this without breaking functionality?
hard
A. Refactor the code while running tests frequently
B. Write new tests before changing code
C. Run tests only after all changes are done
D. Delete old tests to simplify code
Solution
Step 1: Understand the role of refactoring in TDD
Refactoring means improving code structure without changing behavior, done safely with tests as a safety net.
Step 2: Identify the correct practice
Running tests frequently during refactoring ensures no functionality breaks.
Final Answer:
Refactor the code while running tests frequently -> Option A