Ruby Logo

RSpec Testing Framework

Write expressive, behavior-driven specs with describe/it blocks, matchers, shared examples, and Rails helpers.

Home Ruby RSpec Testing Framework

RSpec Testing Framework

RSpec is Ruby's de facto testing framework for behaviour-driven development. Its describe/it DSL mirrors how humans talk about system behaviour, making specs documentation and regression safety net in one. RubyDev ships with Minitest by default, but we frequently incorporate RSpec for BDD-style feature work and examples in documentation.

Describe the behaviour, not implementation

RSpec.describe LearningPath, type: :model do subject(:learning_path) { described_class.new(title: 'Ruby Fundamentals') } describe '#published?' do context 'when published_at is nil' do it 'returns false' do expect(learning_path).not_to be_published end end context 'when published_at is in the past' do before { learning_path.published_at = 1.day.ago } it 'returns true' do expect(learning_path).to be_published end end end end
  • Use one expectation per example unless multi-assert flows add clarity. Separate contexts for different states.
  • `subject` and `let` keep test data DRY. Prefer `let_it_be` (from test-prof) when you need expensive fixtures reused.
  • Tag examples (`type: :request`, `:focus`) to scope entire runs or targeted debugging.

Powerful matchers and doubles

Common matchers

  • `expect(value).to eq(expected)` for strict equality.
  • `expect { ... }.to change(record, :count).by(1)` for side effects.
  • `expect(response).to have_http_status(:ok)` in request specs.
  • `expect(json).to include_json(success: true)` with the json_matchers gem.

Test doubles

analytics = instance_double(AnalyticsService) allow(analytics).to receive(:track).with('lesson_completed', user_id: user.id) subject = LessonCompletion.new(analytics: analytics) subject.call(user) expect(analytics).to have_received(:track).once

Prefer verifying doubles (`instance_double`, `class_double`) so attempts to stub non-existent methods explode instantly.

Rails-specific helpers

  • Request specs: use `get`, `post`, `json_response` helpers to assert on HTTP responses for controllers and API endpoints.
  • System specs: integrate Capybara for full-stack tests. Tag slow specs and run them nightly to balance feedback speed.
  • ActiveJob: wrap expectations with `perform_enqueued_jobs` or `have_enqueued_job` to assert background work (we rely on Sidekiq).
  • FactoryBot: keep factories lean; compose traits rather than building huge default records.

Workflow tips

  1. Run `bundle exec rspec spec/models` for targeted suites; use `--only-failures` to rerun failing specs quickly.
  2. Adopt `spec/support/` for reusable helpers and remember to require files in `rails_helper.rb`.
  3. Fail fast with `--fail-fast` when debugging, but disable it in CI to gather a full failure list for reviewers.
  4. Add metadata like `aggregate_failures: true` when multiple related assertions should surface together.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn rspec testing framework

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!