Ruby Tutorial

Testing

RSpec, Behavior-Driven Development (BDD), and expectations

Why Test?

In Ruby, testing is a fundamental part of the culture. Because Ruby is a dynamic language without a compiler to catch type errors, a strong test suite is essential for ensuring your code works as expected and preventing regressions.

RSpec: The Standard

RSpec is the most popular testing framework for Ruby. It is a "Behavior-Driven Development" (BDD) tool, meaning you write tests that describe the behavior of your code in plain English.
ruby
RSpec.describe User do
  it "is valid with a name" do
    user = User.new(name: "Alpha")
    expect(user).to be_valid
  end

  it "is invalid without a name" do
    user = User.new(name: nil)
    expect(user).to_not be_valid
  end
end
Wait! Notice the syntax: expect(actual).to eq(expected). It's designed to read like a sentence.

Common Matchers

MatcherDescription
eqEquality (==)
be_validChecks if valid? is true
includeChecks if a collection includes a value
changeChecks if an action changes a value
raise_errorChecks if code throws an exception

TDD Lifecycle

  1. Red: Write a test that fails.
  2. Green: Write the minimum code to make it pass.
  3. Refactor: Clean up the code while keeping the tests passing.

Summary

TermDescription
RSpecThe primary BDD framework for Ruby
describeGrouping related tests together
itA single test case
expectThe starting point of an assertion
matcherThe logic that checks the result (eq, be_nil, etc.)
FactoryBotA library for creating test data (Better than fixtures)
Best ForReliability, Documentation, and Confidence
Logic"Describe the behavior, not just the implementation"
SafetyCatch bugs BEFORE they reach production
ModernParallel testing and optimized CI/CD pipelines
StandardHigh test coverage is expected in Ruby projects
IdentityPart of the "Developer Happiness" philosophy