Welcome to the first lesson of our course on Test Driven Development (TDD) in Ruby using RSpec. TDD is an iterative software development process where tests are written before the actual code. This approach enables programmers to focus on the requirements before diving into implementation, ultimately leading to code that is more reliable and maintainable.
In this lesson, we will introduce you to the essential elements of TDD, including the Red-Green-Refactor cycle, which forms the backbone of this methodology. We'll also introduce the tools we'll be using: Ruby and RSpec. Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. RSpec is a popular testing framework in the Ruby community that provides a clean and readable syntax for writing tests. Alternatives like MiniTest exist, but RSpec's expressiveness and community support make it an excellent choice for this course. Let's begin by exploring TDD's core components with an example.
In TDD, the journey begins with writing a test that fails. This "Red" stage allows you to clarify your objective before implementation. Let's start by writing a test for a sum
method, which will eventually add two numbers.
Create a file named math_spec.rb
in the spec
directory:
This test script:
- Requires the
sum
method, which we haven't defined yet.
