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