Unit Testing (RSpec)
Relevant source files
The following files were used as context for generating this wiki page:
The Actionmap project utilizes RSpec as its primary testing framework to ensure code quality, functional correctness, and adherence to style guidelines. The testing infrastructure is integrated into the Ruby on Rails environment, utilizing standard helpers for configuration and execution. The test suite covers not only backend logic but also incorporates automated linting as part of the RSpec execution flow to maintain consistent code standards across Ruby, JavaScript, and HAML files.
Testing is performed in a dedicated environment where the database is treated as "scratch space," wiped and recreated between runs to ensure isolation. The framework integrates with tools like SimpleCov for coverage reporting and DatabaseCleaner for managing state between individual test examples.
Sources: spec/spec_helper.rb, spec/rails_helper.rb:1-10, config/environments/test.rb:5-10
Core Testing Architecture
The RSpec implementation is divided into two primary configuration files: spec_helper.rb for general RSpec settings and rails_helper.rb for Rails-specific testing integrations.
Configuration Helpers
The following table summarizes the roles of the primary configuration files in the test suite:
| File | Purpose | Key Responsibilities |
|---|---|---|
spec_helper.rb | Core RSpec Configuration | Configures expectations, mocks, and global RSpec behaviors. |
rails_helper.rb | Rails Integration | Loads the Rails environment, configures ActiveRecord, and sets up DatabaseCleaner and FactoryBot. |
config/environments/test.rb | Environment Settings | Disables caching, configures Action Mailer for testing, and sets up local storage for tests. |
Sources: spec/spec_helper.rb:23-50, spec/rails_helper.rb:18-30, config/environments/test.rb:1-40
Test Execution Flow
The testing environment follows a specific lifecycle to maintain database integrity and provide consistent results across different test types (e.g., standard specs vs. JavaScript-enabled specs).
This diagram illustrates the lifecycle of a test run, specifically how DatabaseCleaner manages state between examples.
Sources: spec/rails_helper.rb:43-65
Linting and Static Analysis
The project integrates static analysis tools directly into the RSpec suite. This ensures that linter offenses are treated as test failures, enforcing code quality during the continuous integration process.
Ruby Linting (RuboCop)
RuboCop is used to analyze Ruby code for style violations and best practices. The RSpec wrapper executes the command and expects a specific output string indicating zero offenses.
- Command:
bundle exec rubocop - Expected Outcome: Matches
/no offenses detected$/
Sources: spec/linters/rubocop_spec.rb:1-12
JavaScript Linting (ESLint)
JavaScript code is analyzed using ESLint, managed via Yarn scripts defined in package.json.
- Command:
yarn run lint - Target Files:
app/**/*.js,config/**/*.js - Configuration: Uses
eslint-config-airbnb-baseandeslint-plugin-import.
Sources: spec/linters/eslint_spec.rb:1-14, package.json:30-32
HAML Linting (haml-lint)
Template files are inspected using haml-lint to ensure consistency in the view layer.
- Command:
bundle exec haml-lint - Success Condition: Matches
/\s\d+ files? inspected, 0 lints detected\s$/
Sources: spec/linters/haml_lint_spec.rb:1-14
Environment and Dependency Management
The test environment is specifically configured to optimize speed and reliability through the following settings:
- Database Isolation:
ActiveRecord::Migration.maintain_test_schema!is called to ensure the test database matches the current schema before execution. Sources: spec/rails_helper.rb:38-42 - Mocking and Stubbing:
rspec-mocksis configured withverify_partial_doubles = trueto prevent mocking methods that do not exist on real objects. Sources: spec/spec_helper.rb:45-50 - Code Coverage:
SimpleCovis initialized at the start of both RSpec and Cucumber tests, specifically filtering for the Rails environment and excluding thelibdirectory. Sources: spec/rails_helper.rb:5-10, features/support/env.rb:11-15 - Testing Mode:
OmniAuthis set totest_mode = truewith predefined mocks for Google and Github providers to allow testing of authentication flows without external requests. Sources: config/initializers/omniauth.rb:26-38
Summary
The RSpec suite in Actionmap provides a robust foundation for verifying the application's behavior. By combining traditional unit/integration testing with strict static analysis (RuboCop, ESLint, HAML-lint), the project maintains high standards for both functionality and code style. The integration of DatabaseCleaner, FactoryBot, and OmniAuth mocks ensures that the test environment remains predictable and isolated from external dependencies.