Rubocop-rspec: Cop idea: no context for single `it` block

Created on 10 Aug 2016  路  6Comments  路  Source: rubocop-hq/rubocop-rspec

One thing we try to do, when we remember, is to collapse context descriptions when there is a single it block:

# bad
context 'when this thing' do
  it 'does something'
end

# good
it 'does something when this thing' do
end

# also okay
context 'when this thing' do
  it 'does something'
  it 'does something else'
end
cop enhancement

Most helpful comment

Hm I'm not sure how generalizable this is but please try to convince me. For example,

describe SomeAction do
  let(:some_setup) { foo }
  let(:something_else) { bar }

  context 'when performed as a user' do
    let(:user) { ... }

    it 'does not work'
  end

  context 'when performed as an admin' do
    let(:user) { ... }

    it 'works'
  end
end

With that said, I think this might be an easy win if we look for contexts that don't have any additional let, before, etc within it

All 6 comments

Hm I'm not sure how generalizable this is but please try to convince me. For example,

describe SomeAction do
  let(:some_setup) { foo }
  let(:something_else) { bar }

  context 'when performed as a user' do
    let(:user) { ... }

    it 'does not work'
  end

  context 'when performed as an admin' do
    let(:user) { ... }

    it 'works'
  end
end

With that said, I think this might be an easy win if we look for contexts that don't have any additional let, before, etc within it

In those cases we generally won't use a let or before and instead just put them inside the it block:

it 'does not work when performed as a user' do
  user = ...
end

Hm so implementing the "no let/hook/include within context" rule would work for you use case. I think that is the right cop then. Agree?

Hmm, well we might use them in some contexts at the moment, but regardless if there's a rule that prevents having only one it block in a context, we would end up changing any of the context setup to locals in the it.

A good cop IMO would be to check for single context, e.g. when you use context you should always provide the opposite one. If all examples are in the same context, then you are either missing cases or are using a context instead of a describe

Another context-related cop will be to check if context is mixed with examples on the same level.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bquorning picture bquorning  路  4Comments

skorks picture skorks  路  3Comments

steiley picture steiley  路  3Comments

ybiquitous picture ybiquitous  路  7Comments

everaldo picture everaldo  路  3Comments