home / skills / etewiah / property_web_builder / rails-testing

rails-testing skill

/.claude/skills/rails-testing

This skill helps you master Rails testing by guiding test structure, execution, fixtures, and debugging to deliver reliable model, controller, and integration

npx playbooks add skill etewiah/property_web_builder --skill rails-testing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.4 KB
---
name: rails-testing
description: Help with Rails testing including unit tests, integration tests, fixtures, and debugging test failures. Use when working on tests or debugging test issues.
---

# Rails Testing

## Instructions

When helping with Rails testing:

1. **Understand the test structure**
   - Check existing tests in `test/` directory to understand project conventions
   - Look for test patterns in similar files (models, controllers, etc.)
   - Respect the project's test organization and naming conventions

2. **Running tests**
   - Run all tests: `bin/rails test`
   - Run specific file: `bin/rails test test/models/user_test.rb`
   - Run specific test: `bin/rails test test/models/user_test.rb:5` (line number)
   - Run with verbose output: `bin/rails test -v`
   - Use `--fail-fast` to stop on first failure: `bin/rails test --fail-fast`

3. **Test types in Rails**
   - **Models**: Test business logic, validations, associations
   - **Controllers**: Test request/response, status codes, redirects, instance variables
   - **Integration Tests**: Test full workflows across multiple components
   - **Fixtures**: Use for test data setup

4. **Writing effective tests**
   - Use descriptive test names: `test_should_create_valid_user`
   - Test both success and failure cases
   - Keep tests focused and isolated
   - Use fixtures for shared test data
   - Clean up side effects after tests

5. **Debugging test failures**
   - Read error messages carefully - they usually point to the issue
   - Check if test data is set up correctly (fixtures, setup methods)
   - Verify assertions match the actual behavior
   - Use `puts` or `p` to inspect values
   - Check test isolation - tests shouldn't depend on each other

6. **Test database**
   - Rails uses a separate test database
   - Run migrations: `bin/rails db:test:prepare`
   - Check `test/fixtures/` for test data

## Examples

**When user asks: "How do I test this model?"**
→ Create a model test in `test/models/` following existing patterns, test validations and associations

**When user asks: "Why is this test failing?"**
→ Analyze the error, check test data setup, verify assertions, suggest fixes

**When user asks: "Add tests for this controller"**
→ Create controller tests in `test/controllers/`, test CRUD actions and edge cases

**When user asks: "How do I set up test data?"**
→ Suggest fixtures in `test/fixtures/` or setup methods in the test file

Overview

This skill helps Rails developers write, run, and debug tests for models, controllers, and integrations. It focuses on practical, project-aligned guidance for fixtures, test organization, and diagnosing failures so you can keep test suites fast and reliable. Use it when adding new features or fixing regressions that require automated tests.

How this skill works

The skill inspects the test/ directory to detect existing conventions and patterns, then recommends where and how to add tests that match the project style. It provides command-line instructions to run all tests, a single file, or a single example, and walks through common debugging steps for failing tests and test database setup. Recommendations emphasize minimal, focused tests and proper fixture or setup usage.

When to use it

  • Adding or changing model validations, associations, or business logic
  • Creating controller tests for new endpoints or verifying redirects/status codes
  • Writing integration tests that exercise full workflows across multiple components
  • Debugging intermittent or reproducible test failures
  • Setting up or updating test fixtures and the test database

Best practices

  • Follow the project’s existing test organization and naming conventions in test/ to keep consistency
  • Write descriptive test names and test both success and failure cases
  • Keep tests focused and isolated; avoid inter-test dependencies
  • Use fixtures or setup blocks for shared test data and clean up side effects
  • Run tests with bin/rails test and use --fail-fast or -v for faster feedback when debugging

Example use cases

  • Create test/models/user_test.rb to exercise validations and associations following existing patterns
  • Add test/controllers/listings_controller_test.rb to cover create/update/destroy and edge cases
  • Write an integration test for a lead capture flow that touches forms, redirects, and email delivery
  • Diagnose a failing test by checking fixtures, setup methods, and using puts/p to inspect values
  • Prepare the test database with bin/rails db:test:prepare and confirm test/fixtures contain expected data

FAQ

How do I run a single test file or example?

Use bin/rails test test/path/to/file_test.rb or add :line to run a single example, e.g. bin/rails test test/models/user_test.rb:5.

Why is a test failing only in CI and not locally?

Check test database setup, environment-specific config, and for order-dependent tests; ensure fixtures and migrations are applied and tests are isolated.