Usage
You need to tell RuboCop to load the RSpec extension. There are three ways to do this:
RuboCop configuration file
Put this into your .rubocop.yml
:
require: rubocop-rspec
or, if you are using several extensions:
require: - rubocop-rspec - rubocop-performance
Now you can run rubocop
and it will automatically load the RuboCop RSpec
cops together with the standard cops.
RSpec DSL configuration
In case you define aliases for RSpec DSL, i.e. examples, example groups, hooks, or include example statements, you need to configure it so those elements are properly detected by RuboCop RSpec.
# spec/spec_helper.rb
RSpec.configure do |c|
c.alias_example_group_to :detail, :detailed => true
end
# spec/detail_spec.rb
RSpec.detail "a detail" do
it "can do some less important stuff" do
end
end
# .rubocop.yml
RSpec:
Language:
ExampleGroups:
Regular:
- detail
Some libraries extensively define RSpec DSL aliases (e.g. Pundit, Action Policy) or augment existing elements providing the same semantics (e.g. let_it_be
from test-prof
).
Those libraries can provide necessary configuration, but won’t necessarily do so.
If they do, their README will mention that you have to explicitly require their configuration from your .rubocop.yml
file.
# .rubocop.yml
require:
- rubocop-rspec
- test-prof
# or
RSpec:
Language:
Helpers:
- let_it_be
the default merge mode is to inherit, so you won’t remove any of the default settings. |
RuboCop RSpec’s default configuration is a good source of information on what can be configured.
Code Climate
rubocop-rspec
is available on Code Climate as part of the rubocop engine. Learn More.
Inspecting files that don’t end with _spec.rb
By default, rubocop-rspec
only inspects code within paths ending in _spec.rb
or including spec/
. You can override this setting in your config file by setting Include
:
# Inspect files in `test/` directory
RSpec:
Include:
- '**/test/**/*'
# Inspect only files ending with `_test.rb`
RSpec:
Include:
- '**/*_test.rb'
Please keep in mind that Include’s merge mode is set to override the default settings, so if you intend to add a path while keeping the default paths, you should include the default `Include paths in your configuration.
|