spree_dev_tools gem that helps you write Spree-specific tests.
This guide assumes you’ve completed all previous tutorials through Page Builder. You should have a complete
Spree::Brand model with admin, storefront, and SEO features.Setup
Step 1: Set RSpec as the Test Framework
Step 2: Run the spree_dev_tools Generator
spec/support/ directory, including:
- Authorization helpers (
stub_authorization!) - Factory Bot configuration
- Capybara setup for feature tests
- and more…
Step 3: Generate Test Files
spec/models/spree/brand_spec.rb.
Writing Factories
Factories provide a convenient way to create test data. Create a factory for your Brand model:spec/factories/spree/brand_factory.rb
Factory Usage Examples
Writing Model Tests
Model tests verify your business logic, validations, associations, and scopes.spec/models/spree/brand_spec.rb
Testing with Shared Examples
Spree provides shared examples for common patterns. Use them to test metadata support:Testing Decorators
When you extend core Spree models with decorators (see Extending Core Models), test the added functionality:spec/models/spree/product_decorator_spec.rb
Writing Controller Tests
Controller tests verify that your endpoints respond correctly and perform the expected actions.Admin Controller Tests
spec/controllers/spree/admin/brands_controller_spec.rb
Storefront Controller Tests
spec/controllers/spree/brands_controller_spec.rb
Writing Feature Tests
Feature tests (also called system tests) simulate real user interactions using Capybara. They test the full stack including JavaScript.Admin Feature Tests
spec/features/spree/admin/brands_spec.rb
Storefront Feature Tests
spec/features/spree/brands_spec.rb
Test Helpers
Authorization Helper
Usestub_authorization! to bypass authorization checks in admin tests:
wait_for_turbo Helper
When testing with Turbo/Hotwire, usewait_for_turbo to ensure the page has fully loaded:
JavaScript Tests
Addjs: true to tests that require JavaScript:
Running Tests
Best Practices
Use build over create
Use
build instead of create when you don’t need a persisted record. It’s faster because it skips database operations.Use let over instance variables
Prefer
let and let! over instance variables. They’re lazily evaluated and scoped to each example.One assertion per test
Keep tests focused on a single behavior. Use
aggregate_failures if you need multiple assertions.Test behavior, not implementation
Focus on what the code does, not how it does it. This makes tests more resilient to refactoring.
Example: aggregate_failures
Complete Test Suite Structure
After completing this tutorial, your test structure should look like:Related Documentation
- Model Tutorial - Creating the Brand model
- Admin Tutorial - Building the admin interface
- Extending Core Models - Connecting Brands to Products
- Storefront Tutorial - Creating storefront pages
- RSpec Documentation - Official RSpec docs
- Factory Bot Documentation - Factory Bot guide

