1 条规则
When generating RSpec tests, follow these best practices to ensure they are comprehensive, readable, and maintainable:
### Comprehensive Coverage:
- Tests must cover both typical cases and edge cases, including invalid inputs and error conditions.
- Consider all possible scenarios for each method or behavior and ensure they are tested.
### Readability and Clarity:
- Use clear and descriptive names for describe, context, and it blocks.
- Prefer the expect syntax for assertions to improve readability.
- Keep test code concise; avoid unnecessary complexity or duplication.
### Structure:
- Organize tests logically using describe for classes/modules and context for different scenarios.
- Use subject to define the object under test when appropriate to avoid repetition.
- Ensure test file paths mirror the structure of the files being tested, but within the spec directory (e.g., app/models/user.rb → spec/models/user_spec.rb).
## Test Data Management:
- Use let and let! to define test data, ensuring minimal and necessary setup.
- Prefer factories (e.g., FactoryBot) over fixtures for creating test data.
## Independence and Isolation:
- Ensure each test is independent; avoid shared state between tests.
- Use mocks to simulate calls to external services (APIs, databases) and stubs to return predefined values for specific methods. Isolate the unit being tested, but avoid over-mocking; test real behavior when possible.
## Avoid Repetition:
- Use shared examples for common behaviors across different contexts.
- Refactor repetitive test code into helpers or custom matchers if necessary.
## Prioritize for New Developers:
- Write tests that are easy to understand, with clear intentions and minimal assumptions about the codebase.
- Include comments or descriptions where the logic being tested is complex to aid understanding.相关插件
Rails↓ 1.8k
Rails 在 Cursor 中的规则与最佳实践。Rails 8↓ 0
Rails 8 在 Cursor 中的规则、Hotwire 模式、Solid Stack 指导和反模式检测,防止 Rails 7 幻觉并传授地道的 Rails 8 写法。Go↓ 4.5k
Go 在 Cursor 中的规则与最佳实践。Python↓ 3.1k
Python 在 Cursor 中的规则与最佳实践。Playwright↓ 582
Playwright 在 Cursor 中的规则与最佳实践。Scout↓ 6
Scout 在 Cursor 中的规则与最佳实践。wopee-mcp↓ 4
用于 Wopee.io 自主端到端测试的 MCP server,分析 Web 应用、生成并执行基于 Playwright 的功能测试并校验结果。SilbercueSwift↓ 3
iOS 模拟器自动化 MCP server,支持构建、测试、20ms 截图、UI 交互、日志与 git,直接集成 WDA 无需 Appium,含 55 个工具及免费与 Pro 套餐。