问题
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!
回答1:
I would recommend making a pull request to RSpec changing their templates to be Rubocop-compliant.
回答2:
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 replacing or
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):
bundle exec rubocop -F -a --only \
Style/TrailingBlankLines,\
Style/TrailingWhitespace,\
Style/SymbolProc,\
Style/SpaceInsideHashLiteralBraces,\
Style/SpaceInsideBrackets,\
Style/SpaceInsideBlockBraces,\
Style/SpaceInsideParens,\
Style/SpaceAroundEqualsInParameterDefault,\
Style/Semicolon,\
Style/StringLiterals,\
Style/SpaceAfterComma,\
Style/SpaceAroundOperators,\
Style/AlignHash,\
Style/AlignParameters,\
Style/AndOr,\
Style/EmptyLines,\
Style/HashSyntax,\
Style/RedundantSelf,\
Style/ColonMethodCall,\
Style/IndentationWidth,\
Style/MultilineIfThen,\
Style/NegatedIf,\
Style/PercentLiteralDelimiters,\
Style/BarePercentLiterals,\
Style/BlockEndNewline,\
Style/CollectionMethods,\
Style/CommentIndentation,\
Style/IndentationConsistency,\
Style/ParenthesesAroundCondition,\
Style/CaseIndentation,\
Lint/EndAlignment,\
Style/LeadingCommentSpace,\
Style/MutableConstant,\
Style/NumericLiteralPrefix,\
Style/MultilineIfModifier,\
Style/TrailingCommaInLiteral,\
Style/IndentArray,\
Style/AlignArray,\
Style/MultilineArrayBraceLayout,\
Style/ElseAlignment,\
Lint/EndAlignment,\
Style/MultilineMethodCallBraceLayout,\
Style/ClosingParenthesisIndentation,\
Style/MultilineHashBraceLayout,\
Lint/BlockAlignment,\
Style/IndentHash,\
Style/LeadingCommentSpace,\
Style/SpaceBeforeComma,\
Style/SpaceBeforeSemicolon,\
Style/Tab,\
Style/MultilineMethodCallIndentation
来源:https://stackoverflow.com/questions/25138264/rspec-rubocop-ruby-style-guide