when telling the rails scaffold controller to generate rspec tests the generated files don't follow the Ruby Style Guide and thus rubocop throws errors on these.
Only a couple of "cops" fail. Style/HashSyntax, Style/StringLiterals, Style/MethodCallParentheses, Style/SpaceInsideHashLiteralBraces, Style/Blocks.
I do understand that some of these cops are just style preferences like for example Style/StringLiterals (Prefer single-quoted strings when you don't need string interpolation or special symbols.)
Nevertheless I'd like my tests to conform to rubocop. Is anyone aware of a gem that replaces the rspec-rails templates (e.g. controller_spec.rb) with ones that conform to the Ruby Style Guide / rubocops Cops? Or of any undocumented (or documented) rspec / rspec-rails feature that solves my problem?
A work around is running
bundle exec rubocop spec --auto-correct
after generating new test files, but I'd prefer leaving out that step.
Any help or pointers are greatly appreciated!
I simply let rubocop autocorrect stuff like that. I usually have a script
script/rubocop-autocorrect.rb
or something where I explicitely list all the cops that should be corrected. I have never had an error introduced by it; there has only been one case where rubocop failed to correct something (related to replacingor
by||
in a very weird case). It simply did nothing, it did not break the code.The complete script looks like that (nothing special, just my preferences):
I would recommend making a pull request to RSpec changing their templates to be Rubocop-compliant.