0
0
Rubyprogramming~30 mins

Test-driven development workflow in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Test-driven development workflow
📖 Scenario: You are building a simple calculator program that adds two numbers. You will use test-driven development (TDD) to create the calculator step by step.
🎯 Goal: Create a Ruby program that adds two numbers using TDD. You will first write a test, then write the code to pass the test, and finally print the result.
📋 What You'll Learn
Create a test method named test_addition that checks if adding 2 and 3 equals 5
Create a method named add that takes two numbers and returns their sum
Call the add method with 2 and 3 and store the result in a variable named result
Print the value of result
💡 Why This Matters
🌍 Real World
Test-driven development helps programmers write reliable code by writing tests before the actual code. This reduces bugs and improves confidence.
💼 Career
Many software development jobs require knowledge of TDD to ensure code quality and maintainability.
Progress0 / 4 steps
1
Write the test method
Write a method named test_addition that uses raise to check if add(2, 3) equals 5. Use raise "Test failed" unless add(2, 3) == 5 inside the method.
Ruby
Need a hint?

Define test_addition method and use raise with a condition that calls add(2, 3) and checks if it equals 5.

2
Create the add method
Create a method named add that takes two parameters a and b and returns their sum using a + b.
Ruby
Need a hint?

Define add method with two parameters and return their sum.

3
Run the test and store the result
Call the test_addition method to run the test. Then call add(2, 3) and store the result in a variable named result.
Ruby
Need a hint?

Call test_addition to run the test, then assign add(2, 3) to result.

4
Print the result
Print the value of the variable result using puts result.
Ruby
Need a hint?

Use puts result to display the sum.