返回顶部
r

rails-tdd-standards

RSpec testing standards and best practices for Rails applications. Use when writing new tests, reviewing test quality, debugging factory errors, setting up FactoryBot, or enforcing single-expectation patterns. Also use when a test fails due to factory misconfiguration, wrong association keys, or missing role traits. Triggers on phrases like "write a test", "add specs", "factory error", "test is failing", "how should I test this", or when reviewing test code in a Rails project.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.1
安全检测
已通过
174
下载量
0
收藏
概述
安装方式
版本历史

rails-tdd-standards

# Rails TDD Standards Best practices for writing clean, reliable RSpec tests in Rails applications. ## Core Principle: Single Expectation One assertion per test. Tests should read like specifications — each `it` block verifies exactly one thing. ```ruby # ✅ Correct it { is_expected.to validate_presence_of(:email) } it { is_expected.to belong_to(:user) } # ❌ Wrong — too many expectations in one test it "validates the user" do expect(user).to validate_presence_of(:email) expect(user).to validate_presence_of(:name) expect(user).to be_valid end ``` ## FactoryBot Patterns ### Always use role traits ```ruby # ✅ Correct create(:user, :admin) create(:user, :driver) create(:user, :patron) # ❌ Wrong — missing role context create(:user) ``` ### Association keys matter Check your factory definitions carefully. Wrong keys cause silent failures. ```ruby # ✅ Example — if your factory uses owner: create(:profile, owner: user) # ❌ Wrong key create(:profile, user: user) # fails if factory expects owner: ``` ### Always set required associations When a model requires a specific association to be valid, always set it explicitly — don't rely on factory defaults when they might be nil or wrong. ```ruby # ✅ Explicit — clear intent, no surprises let(:record) do create(:model, required_association: other_record) end # ❌ Implicit — may break if factory default changes let(:record) { create(:model) } ``` ### Use `described_class` not hardcoded class names ```ruby # ✅ subject { described_class.new(params) } # ❌ subject { MyService.new(params) } ``` ## Common FactoryBot Gotchas ### Join tables without primary key Tables with `id: false` can't use `.last` or `.first`. ```ruby # ✅ Use a scoped query record = JoinModel.find_by(field_a: a, field_b: b) # ❌ Will raise ActiveRecord::MissingRequiredOrderError record = JoinModel.last ``` ### RecordInvalid from missing role/trait If you see `Validation failed: X must have Y role` — you're missing a trait on the user factory. ```ruby # ✅ user = create(:user, :driver) # ❌ causes "must be a driver" validation error user = create(:user) ``` ## Spec Structure ```ruby RSpec.describe MyClass do # Subject subject(:instance) { described_class.new(params) } # Shared setup let(:user) { create(:user, :admin) } # Group by behavior describe "#method_name" do context "when condition is true" do it "does the expected thing" do expect(instance.method_name).to eq(expected) end end context "when condition is false" do it "does something else" do expect(instance.method_name).to be_nil end end end end ``` ## Mocking & Stubbing ```ruby # Stub a method allow(object).to receive(:method_name).and_return(value) # Stub and verify it was called expect(object).to receive(:method_name).once # Stub HTTP calls (WebMock) stub_request(:post, "https://api.example.com/endpoint") .to_return(status: 200, body: { result: "ok" }.to_json) # Allow localhost for system tests (if using WebMock) WebMock.disable_net_connect!(allow_localhost: true) ``` ## Service Object Testing ```ruby RSpec.describe MyService do describe "#call" do context "with valid params" do it "returns the expected result" do result = described_class.new(valid_params).call expect(result).to be_a(ExpectedClass) end it "creates the expected record" do expect { described_class.new(valid_params).call } .to change(Record, :count).by(1) end end context "with invalid params" do it "returns false" do expect(described_class.new(invalid_params).call).to be(false) end end end end ``` ## Running Tests ```bash # Run full suite bundle exec rspec # Run specific file bundle exec rspec spec/models/user_spec.rb # Run specific line bundle exec rspec spec/models/user_spec.rb:42 # Run only failures from last run bundle exec rspec --only-failures # Run with documentation format bundle exec rspec --format documentation ``` ## See Also - `references/factory-patterns.md` — advanced FactoryBot patterns - `references/system-specs.md` — Capybara / browser testing setup

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 rails-tdd-standards-1776114362 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 rails-tdd-standards-1776114362 技能

通过命令行安装

skillhub install rails-tdd-standards-1776114362

下载 Zip 包

⬇ 下载 rails-tdd-standards v1.0.1

文件大小: 4.91 KB | 发布时间: 2026-4-17 15:54

v1.0.1 最新 2026-4-17 15:54
Fix: Added clawdbot runtime metadata

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部