Rubocop-rspec: rspec feature spec has no described_class

Created on 28 Aug 2014  路  14Comments  路  Source: rubocop-hq/rubocop-rspec

If a feature or request spec is in the features directory will not have a described_class:

describe "a simple feature" do
end

The response is

The first argument to describe should be the class or module being tested.

Could we turn off this check for feature specs pls?

question

Most helpful comment

yes, i get it

RSpec/DescribeClass:
  Exclude:
    - spec/features/*

All 14 comments

You can easily turn this off by using a .rubocop.yml. In this you can turn it off at all, or just for a specific folder.

Does this helps you?

yes, i get it

RSpec/DescribeClass:
  Exclude:
    - spec/features/*

Hmmm... what if you only want this feature shut off for specific classes but the rest of the cops active on those specs. If you exclude the entire directory you omit everything right?

As @testbrian mentioned you can define excludes for cops based on any file glob. You can also do # rubocop:disable RSpec/DescribeClass inline in a single source file.

Correct, I am aware of that, but you would have to put an enable disable around every describe when you want to only disable that specific feature on named classes. TBH, that seems a bit extreme right?

I can't really tell what you mean to be honest. Can you give an example of a the sort of code you are concerned about?

Coolio.

Let me try to show some pseudo code here:

describe 'myapp:data:load ->' do
  include Cancun::Highline
  before { init_highline_test }
  include_context 'rake'

  let(:task_name) { 'myapp:data:load:from' }
  let(:path_to_task) { 'tasks/data' }

  describe ':from', focus: true do
    it 'processes from a specified starting point' do

    end
  end
end

I would think we could keep the cops for the rest of the spec but omit only the describe.

Ok I think I'm following. So in this case

RSpec/DescribeClass:
  Exclude:
    - spec/tasks/myapp_data_load.rb

Would only disable the DescribeClass cop for the file in question assuming spec/tasks/myapp_data_load.rb is the path of the code in your example there

Awesome, thank you so much.

Feature tests seem to be excluded if you explicitly set type: :feature on them.

While disabling this cop on a feature folder works for me, I would like to see the Cop actually trying to understand if a spec is not a class description. I am using config.infer_spec_type_from_file_location! for rspec to set the test type by convetion rather than explicitly (and redundantly) on each test. Would it be possible to detect that automatically and switch the cop of for feature, system and maybe request specs?

Sure, that would be a nice addition.
Would you like to hack on it @TimMoser92 ?

Off the top of my head:

  • check existing cops that check metadata
  • in the initializer, figure out how to fetch this data from spec/spec_helper.rb (or spec/rails_helper.rb). I guess this is very unreliable and will fail with any dynamic declaration. So we'll need our own mapping in the configuration to repeat those for RSpec
  • inject metadata in examples/example groups; not sure if there should be a distinction between explicitly defined and inherited metadata

I will try to have a look at it later today. Thank you for the pointers, @pirj !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MadhuriShres picture MadhuriShres  路  5Comments

ybiquitous picture ybiquitous  路  7Comments

pirj picture pirj  路  4Comments

sauloperez picture sauloperez  路  7Comments

skorks picture skorks  路  3Comments