Rubocop-rspec: Allow to catch RSpec/ExpectInHook offenses in the setup block

Created on 4 Nov 2019  路  4Comments  路  Source: rubocop-hq/rubocop-rspec

RSpec/ExpectInHook is a great cop that allows us to detected expectation usage in the hook. Unfortunately, it does not work with setup block which is a part of rspec-rails gem.

Here is a valid Rspec example that won't be detected via RuboCop.

setup do
  expect(something).to eq 'foo'
end

Probably, for fixing that issue we just need to extend the language dictionary and add new hook. https://github.com/rubocop-hq/rubocop-rspec/blob/0036374b8bd3cea61d9a61a911cbaeaf3541883e/lib/rubocop/rspec/language.rb#L85-L97

P.S. Probably, also we can add a new cop which can detect setup usage and suggest to use before block.

All 4 comments

Related to https://github.com/rubocop-hq/rubocop-rspec/issues/333
In addition to aliases to examples/example groups, consider also configuring hooks and matchers that we treat as "built-in".

Would you like to take a stab on this @roman-dubrovsky ?
It encompasses renaming ALL in Language::Hooks to BUILTIN, adding a CUSTOM that would load custom hooks from AllCops/RSpec, and adding an ALL as a union of BUILTIN and CUSTOM. Plus, documentation on how to configure it in the project using RuboCop RSpec.

That would be a solid first step to solve a number of outstanding issues related to RSpec DSL extensions that RuboCop RSpec previously did not consider.

@roman-dubrovsky let me try to handle this. I will work on it @pirj

@roman-dubrovsky I'm closing this, since RSpec Rails doesn't seem to recommend using setup, but rather use more conventional before, after and around hooks. setup comes from Minitest, and RSpec Rails wraps it into hooks.

In any case, this will be handled in #1077

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bquorning picture bquorning  路  4Comments

bbatsov picture bbatsov  路  7Comments

bgeuken picture bgeuken  路  5Comments

yorkxin picture yorkxin  路  8Comments

andyw8 picture andyw8  路  4Comments