diff --git a/lib/rubocop/cop/rspec/described_class.rb b/lib/rubocop/cop/rspec/described_class.rb index a5e943889..cbe453858 100644 --- a/lib/rubocop/cop/rspec/described_class.rb +++ b/lib/rubocop/cop/rspec/described_class.rb @@ -8,7 +8,8 @@ module RSpec # If the first argument of describe is a class, the class is exposed to # each example via described_class. # - # This cop can be configured using the `EnforcedStyle` option + # This cop can be configured using the `EnforcedStyle` and `SkipBlocks` + # options. # # @example `EnforcedStyle: described_class` # # bad @@ -32,6 +33,27 @@ module RSpec # subject { MyClass.do_something } # end # + # There's a known caveat with rspec-rails's `controller` helper that + # runs its block in a different context, and `described_class` is not + # available to it. `SkipBlocks` option excludes detection in all + # non-RSpec related blocks. + # + # To narrow down this setting to only a specific directory, it is + # possible to use an overriding configuration file local to that + # directory. + # + # @example `SkipBlocks: true` + # # spec/controllers/.rubocop.yml + # # RSpec/DescribedClass: + # # SkipBlocks: true + # + # # acceptable + # describe MyConcern do + # controller(ApplicationController) do + # include MyConcern + # end + # end + # class DescribedClass < Cop include ConfigurableEnforcedStyle diff --git a/manual/cops_rspec.md b/manual/cops_rspec.md index cc2479049..d5e5618eb 100644 --- a/manual/cops_rspec.md +++ b/manual/cops_rspec.md @@ -369,7 +369,17 @@ Checks that tests use `described_class`. If the first argument of describe is a class, the class is exposed to each example via described_class. -This cop can be configured using the `EnforcedStyle` option +This cop can be configured using the `EnforcedStyle` and `SkipBlocks` +options. + +There's a known caveat with rspec-rails's `controller` helper that +runs its block in a different context, and `described_class` is not +available to it. `SkipBlocks` option excludes detection in all +non-RSpec related blocks. + +To narrow down this setting to only a specific directory, it is +possible to use an overriding configuration file local to that +directory. ### Examples @@ -399,6 +409,20 @@ describe MyClass do subject { MyClass.do_something } end ``` +#### `SkipBlocks: true` + +```ruby +# spec/controllers/.rubocop.yml +# RSpec/DescribedClass: +# SkipBlocks: true + +# acceptable +describe MyConcern do + controller(ApplicationController) do + include MyConcern + end +end +``` ### Configurable attributes