Add Rubocop RSpec cops
Over the last few weeks I’ve been on a slight rubocop rampage with some of my older ruby based projects. Running a static code analyser like rubocop over code originally written for ruby 1.9.3 has been a nice refresher in how idioms change over time. Once I learned to accept I’m going to use the occasional override, or even just turn certain rules off, I came to agree that it’s helped improve my code. Or at the very least made me more deliberate about certain parts of it. Looking through my RSpec tests while cleaning up the few violations the base rubocop gem flagged I wondered if there was anything to help me with the test scaffolding itself, and then I found [rubocop-rspec](https://github.com/rubocop-hq/rubocop- rspec).
The rubocop-rspec
gem provides additional focused rules, which rubocop
calls “cops”, to help you standardise your use of rspecs many features.
In fact it provides a
lot of rspec specific cops.
On code as old as mine it can feel like the initial run is triggering
most of them.
Adding an extension to rubocop is as easy as you’d hope. All you need to
do it either add it to your Gemfile
-
gem 'rubocop-rspec', '~> 1.43.0', require: false
Or to your gemspec
spec.add_development_dependency 'rubocop-rspec', '~> 1.43.0'
and enable it inside your .rubocop.yml
configuration file.
versions:
- rubocop
- rubocop-rspec
A quick bundle install
later and you too can bask in an itemised list
of your coding failures. I’ve only used it in a few repositories to
start with but so far I’ve found it’s helped me tighten up test
descriptions and remove some redundant wording, leaving the actual tests
a little clearer. It’s not going to completely redefine how you write
your tests but it will make you a little more mindful of their contents
and in these days of isolated remote working it’s always nice to have a
second set of (automated) eyes checking your work.