discourse-writing-rspec-tests

Write and structure RSpec tests for Discourse core, plugins, themes, and theme components.

47.8k|9.0k|Updated Jan 12, 2013
One-click install
npx skills add https://github.com/discourse/discourse --skill discourse-writing-rspec-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: discourse-writing-rspec-tests
Source: https://github.com/discourse/discourse/tree/main/.skills/discourse-writing-rspec-tests
Command: npx skills add https://github.com/discourse/discourse --skill discourse-writing-rspec-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Writing tests for Discourse requires following specific conventions for fabricators, page objects, request specs, and system tests. This Skill provides the patterns and structure needed to create consistent, maintainable RSpec tests across Discourse core, plugins, themes, and theme components.

Core Features & Use Cases

  • Testing Principles: Enforces behavior-focused testing with public boundary assertions, minimal mocking, and readable test structure limited to two nesting levels.
  • Specialized Test Types: Covers request specs grouped by controller action, system tests with page objects and Capybara matchers, and theme tests with upload helpers.
  • Tracking Helpers: Provides DiscourseEvent.track_events, MessageBus.track_publish, and track_sql_queries for asserting side effects without mocking internals.
  • Use Case: When adding a new controller endpoint to a Discourse plugin, use this Skill to generate a request spec that signs in a user, posts to the endpoint, and asserts on response status, parsed body, and persisted state.

Quick Start

Write an RSpec request spec for the Discourse bookmarks controller create action following the project's testing conventions.

Frequently Asked Questions about discourse-writing-rspec-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write RSpec tests for Discourse plugins?

Structure tests by public entry point using describe blocks like ".call" or "#execute", use fab! for shared records, and assert on observable outcomes like persisted state or response bodies. Follow the same conventions as Discourse core specs.

How do I write request specs for Discourse controllers?

Place specs in spec/requests/ named after the controller, group by action with describe "#action_name", sign in users inside the action block, and assert on response status, parsed_body, and persisted state rather than internal method calls.

What is the difference between fab! and let in Discourse specs?

fab! uses TestProf let_it_be for records shared across examples and cannot depend on per-example let values. Use let for lazy per-example values, let! when a record must exist before the action, and inline Fabricate for single-example records.

How do I test Discourse themes and theme components?

Place system tests in the theme's spec/system directory, use upload_theme or upload_theme_component helpers, and run them from the Discourse root with bin/rspec tmp/themes/my-theme/spec/system after cloning into tmp/themes.

Why do Discourse system tests fail intermittently?

Flakiness usually comes from racing transient UI states or asserting on stale element references. Use Capybara matchers like has_css? that re-query the DOM, hold transient states with with_pending_requests, and never negate has_css? with a boolean.

How do I assert on DiscourseEvent or MessageBus side effects in tests?

Wrap the operation in DiscourseEvent.track_events or MessageBus.track_publish block helpers, which capture triggered events or published messages and return them for assertion, avoiding mocks of internal trigger or publish calls.