Rubocop-rspec: Users should be able to globally configure example group and includes language

Created on 26 Feb 2017  路  9Comments  路  Source: rubocop-hq/rubocop-rspec

As described in #321, if a user has some custom DSL in their specs then a handful of cops produce false positives. For example

RSpec.describe Authentication do
  role :user do
    it 'accepts the request' do
      expect(request).to be_accepted
    end
  end

  role :admin do
    it 'accepts the request' do
      expect(request).to be_accepted
    end
  end
end

In RSpec you can do something like this

RSpec.configure do |c|
  c.alias_example_group_to :detail, :detailed => true
end

and of course since it is plain old ruby you can do something like this (not sure if this would specifically work)

module AuthenticationSpecs
  def role(type)
    context "when user is a #{type}" do
      let(:user) { create(type) }

      yield
    end
  end
end

RSpec.configure do |config|
  config.extend(AuthenticationSpecs)
end

To handle stuff like this properly, we should support something like this

AllCops:
  RSpec:
    CustomExampleGroups:
    - detail
    - role

which all cops would respect.

enhancement help wanted

Most helpful comment

@sl4vr Thanks a lot!
Really sorry for the huge delay. Moved the review of this to the top of my list.

All 9 comments

It also happen with permissions. For example:

  permissions :create?, :can_create_package_in?, :unlock? do
    it { expect(subject).to permit(user, project) }
  end

  permissions :local_project_and_allowed_to_create_package_in? do
    it { expect(subject).to permit(user, project) }
  end

Just seen it in Rubocop 0.58.2

permissions is from the Pundit gem鈥檚 RSpec DSL: https://www.rubydoc.info/gems/pundit/Pundit%2FRSpec%2FDSL:permissions

As @backus mentioned above, we鈥檇 have to make a configurable list of DSL aliases, and stick to using that list of aliases throughout our cops.

Additionally, I imagine a cop that would specifically watch over alias_example_group_to/alias_example_to/alias_object_eq_to occurrences in spec_helper.rb to make sure all of them are added AllCops/RSpec configuration, or explicitly ignored in it.

Not sure how to deal with aliases defined in specs directly, does Language have to track that dynamically?

Have the same issue with pundit. Is it going to be released?

@denys-husiev There's nothing to release yet. A proof-of-concept is WIP now #894, you may jump in if you'd love to help. And after we would still need to make the rest parts of the Language module to be configurable. Any help on this front is much appreciated.

I made some POCs here: https://github.com/rubocop-hq/rubocop-rspec/pull/921#issuecomment-637012579.
I would like to go ahead, but review and agreement on implementation are needed from @rubocop-hq maintainers.

@sl4vr Thanks a lot!
Really sorry for the huge delay. Moved the review of this to the top of my list.

RSpec DSL alias configuration has been implemented in #956, and released in 2.0.

A reference implementation on how to allow third-party gems to configure their RSpec DSL configuration for RuboCop RSpec is tracked in #1077

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgeuken picture bgeuken  路  5Comments

skorks picture skorks  路  3Comments

bbatsov picture bbatsov  路  7Comments

QQism picture QQism  路  3Comments

roman-dubrovsky picture roman-dubrovsky  路  4Comments