rubocop-rspec
rubocop-rspec copied to clipboard
`RSpec/VariableName` & `RSpec/VariableDefinition` complain on non-rspec `subject` method
# `email_spec.rb` or whatever
Mail.new do
subject 'This is a test email'
end
which is mail gem DSL for creating new emails, leads to
$ rubocop -rrubocop-rspec -V
1.14.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 3.0.0 x86_64-linux)
- rubocop-rspec 2.2.0
$ rubocop -rrubocop-rspec email_spec.rb
Inspecting 1 file
C
Offenses:
email_spec.rb:3:11: C: RSpec/VariableDefinition: Use symbols for variable names.
subject 'This is a test email'
^^^^^^^^^^^^^^^^^^^^^^
email_spec.rb:3:11: C: RSpec/VariableName: Use snake_case for variable names.
subject 'This is a test email'
^^^^^^^^^^^^^^^^^^^^^^
1 file inspected, 2 offenses detected
Hey @ojab !
Indeed, this is problematic. mail testing doc.
I'm thinking if limiting those cops inspection to only subject that is a defied directly in an example group block would result in false positives in some other DSL. I believe your current project is rich in examples of various RSpec DSLs. What do you think?
This one i a new project, so not many specs yet, but you know that there is more projects here :)
I guess restricting subject checks to the example groups makes sense.
I think we can lower the false positives by:
- limiting the check to block and blockpass nodes
and/or
- ignoring rspec DSL inside class/module/struct .new blocks (we can go for the extra mile to check if the defined class icludes the RSpec DSL, but that would be too edgy)
limiting the check to block and blockpass nodes
Brilliant idea, that should do it.
I couldn't quickly find if mail supports subject with a block, apparently it doesn't.
I couldn't quickly find if mail supports subject with a block, apparently it doesn't.
It does, but only with explicit receiver, so should be fine
> Mail.new { |mail| mail.subject = 'foo' }.subject
=> "foo"
@ojab Would you like to send a PR to fix this? #1224 might give some hints for the change.
@ojab Ping
I'd like to, but lack of capacity :crying_cat_face: