Back to Rules
Ruby

RSpec Best Practices: Essential Tips for Readable Ruby Tests

Claude Directory November 29, 2025
0 copies 2 downloads

Elevate your Ruby testing with proven RSpec best practices for comprehensive coverage, clear structure, and maintainable code. Perfect for Rails developers seeking efficient, isolated tests.

Rule Content
**Do:**
- **Achieve full test coverage** by including happy paths, edge cases, invalid data, and error scenarios for every method. *Example:* For a user validation method, test valid emails, empty fields, and malformed inputs.
- **Enhance readability** with descriptive `describe`, `context`, and `it` block names, and always use `expect` syntax. *Example:* `expect(user.valid?).to be true` instead of `user.valid?.should be_true`.
- **Organize logically** using `describe` for classes/modules, `context` for scenarios, and `subject` to reuse the test object. *Example:* `subject { described_class.new(params) }` to avoid repeating instantiation.
- **Manage test data efficiently** with `let` or `let!` for lazy setup and FactoryBot for realistic objects over static fixtures. *Example:* `let(:user) { create(:user) }`.
- **Ensure test isolation** by making each test self-contained, using mocks for external services and stubs for methods. *Example:* `allow(UserService).to receive(:call).and_return(mock_data)`.
- **Eliminate duplication** via shared examples or custom helpers. *Example:* `shared_examples 'validates presence' do |field| it { is_expected.not_to allow_value(nil).for(field) } end`.
- **Support newcomers** with self-explanatory tests and comments on tricky logic. *Example:* Add `# Tests edge case where API returns 500` above complex mocks.

**Don't:**
- **Skip edge cases** – never test only typical inputs. *Counter-example:* Omitting nil checks leads to runtime surprises.
- **Use vague names** or `should` syntax, which reduces clarity. *Counter-example:* Avoid `it 'works'`; use `it 'returns user data for valid ID'`.
- **Nest tests deeply** or mismatch file paths. *Counter-example:* Don't put `spec/controllers/users_spec.rb` for `app/models/user.rb`; follow `spec/models/user_spec.rb`.
- **Rely on fixtures** or global setup, causing brittle tests. *Counter-example:* Hardcoded YAML data breaks with schema changes.
- **Share state** between tests or over-mock internals. *Counter-example:* Modifying a shared database connection affects unrelated tests.
- **Repeat code** without refactoring. *Counter-example:* Copy-pasting 10-line setups instead of using `let` blocks.
- **Assume deep knowledge** – avoid tests needing codebase tours. *Counter-example:* Unexplained complex stubs confuse junior devs.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory