DEV Community

How to minimize RSpec `describe`/`context` nesting

Seiei Miyagi on January 18, 2019

Deeply nested specs Did you ever write the deeply nested code in RSpec? I wrote a lot honestly. In the application code, I avoid to wri...
Collapse
 
danielgomezrico profile image
Daniel Gomez

I like to nest things too, I believe that some tests share the same preconditions should be nested, I don't see why it is hard to read, maybe showing some examples on how that is hard to read.

I saw a lot of "plain tests" which are also hard to read, so I don't believe that is a reason to sacrifice this.

Collapse
 
philnash profile image
Phil Nash

I have found myself nesting things more and while I don't necessarily think it's as bad as your colleagues do, this has been a good reminder about shared contexts. Thanks!

Collapse
 
hanachin profile image
Seiei Miyagi

I don't necessarily think it's as bad as your colleagues do

ThanksπŸ‘

We try rubocop-rspec with following setting at new project.
rubocop-rspec.readthedocs.io/en/la...

# .rubocop.yml
RSpec/NestedGroups:
  Max: 2

The specs became so Ghekin-ish 🀣

Collapse
 
bunufi profile image
Dainius

This is a really good idea! Thanks for writing up and sharing.