【问题标题】:RSpec & Rubocop / Ruby Style GuideRSpec & Rubocop / Ruby 风格指南
【发布时间】:2014-08-05 11:57:56
【问题描述】:

当告诉 Rails 脚手架控制器生成 rspec 测试时,生成的文件不遵循 Ruby Style Guide,因此 rubocop 会在这些文件上抛出错误。

只有几个“警察”失败了。 Style/HashSyntax、Style/StringLiterals、Style/MethodCallParentheses、Style/SpaceInsideHashLiteralBraces、Style/Blocks。

我明白其中一些警察只是样式偏好,例如 Style/StringLiterals(当您不需要字符串插值或特殊符号时,首选单引号字符串。)

不过,我希望我的测试符合 rubocop。有谁知道用符合 Ruby Style Guide / rubocops Cops 的模板替换 rspec-rails 模板(例如controller_spec.rb)的 gem?或者解决我的问题的任何未记录(或记录)的 rspec / rspec-rails 功能?

正在运行解决方法

bundle exec rubocop spec --auto-correct

在生成新的测试文件之后,但我宁愿省略这一步。

非常感谢任何帮助或指点!

【问题讨论】:

    标签: rspec rubocop


    【解决方案1】:

    我建议向 RSpec 提出拉取请求,将其模板更改为符合 Rubocop 标准。

    【讨论】:

      【解决方案2】:

      我只是让 rubocop 自动更正这样的东西。我通常有一个脚本script/rubocop-autocorrect.rb 或者我明确列出所有应该纠正的警察的东西。我从来没有遇到过它引入的错误;只有一种情况是 rubocop 未能纠正某些问题(在一个非常奇怪的情况下,与将 or 替换为 || 有关)。它只是什么也没做,它没有破坏代码。

      完整的脚本是这样的(没什么特别的,只是我的喜好):

      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
      

      【讨论】:

      • 可以分享一下脚本吗? :-)
      • 当然,享受。 @sekmo
      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2017-11-26
      • 2011-01-13
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多