Code Coverage with SimpleCov
This project uses SimpleCov to track code coverage for the test suite.
Usage
Running Tests with Coverage
To run tests with coverage enabled:
# Run all tests with coverage
COVERAGE=true bundle exec rspec
# Run specific tests with coverage
COVERAGE=true bundle exec rspec spec/models/
# Run a specific test file with coverage
COVERAGE=true bundle exec rspec spec/models/restaurant_spec.rb
Viewing Coverage Reports
After running tests with coverage:
- Open
coverage/index.htmlin your browser to view the HTML report - The report shows:
- Overall line and branch coverage percentages
- Coverage by file and directory
- Detailed line-by-line coverage for each file
Coverage Configuration
SimpleCov is configured with the following settings:
- Minimum Coverage: 10% (adjustable in
spec/spec_helper.rb) - Excluded Directories: spec/, config/, vendor/, tmp/, engines/, gems/, doc/, docs/, log/, public/, db/migrate/, bin/, script/
- Grouped Coverage: Controllers, Models, Services, Workers, Concepts, Serializers, Policies, Helpers, Mailers, Validators, Lib
- Branch Coverage: Enabled for Ruby 2.5+
Configuration Files
spec/spec_helper.rb: Main SimpleCov configuration.simplecov: Additional project-specific configuration.gitignore: Coverage directory is already excluded from git
CI Integration
The coverage reports are generated in JSON format for potential CI integration. Coverage data is stored in coverage/.resultset.json.
Troubleshooting
If you encounter issues:
- Make sure you’re running tests with
COVERAGE=trueenvironment variable - Check that SimpleCov is properly required at the top of
spec/spec_helper.rb - Ensure tests are actually running (SimpleCov only tracks executed code)
Best Practices
- Run coverage on the full test suite for accurate results
- Individual spec files may show low coverage due to not exercising the full codebase
- Focus on improving coverage for critical business logic
- Use coverage reports to identify untested code paths