-
-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RSpec/ExpectChange doesn't work well with nested class #1037
Comments
ruby-parse shows AST like below for
RSpec::ExpectChange checks the AST with regex
|
I see what you mean for It seems to be relatively easy to fix: def_node_matcher :expect_change_with_arguments, <<-PATTERN
- (send nil? :change ({const send} nil? $_) (sym $_))
+ (send nil? :change {$const (send nil? $_)} (sym $_))
PATTERN
def_node_matcher :expect_change_with_block, <<-PATTERN
(block
(send nil? :change)
(args)
- (send ({const send} nil? $_) $_)
+ (send {$const (send nil? $_)} $_)
)
PATTERN plus expect_change_with_arguments(node) do |receiver, message|
- msg = format(MSG_CALL, obj: receiver, attr: message)
+ msg = format(MSG_CALL, obj: receiver.source, attr: message)
add_offense(node, message: msg) do |corrector|
- replacement = "change { #{receiver}.#{message} }"
+ replacement = "change { #{receiver.source}.#{message} }"
corrector.replace(node, replacement)
end
end
@ lib/rubocop/cop/rspec/expect_change.rb:67 @ module RuboCop
return unless style == :method_call
expect_change_with_block(node) do |receiver, message|
- msg = format(MSG_BLOCK, obj: receiver, attr: message)
+ msg = format(MSG_BLOCK, obj: receiver.source, attr: message)
add_offense(node, message: msg) do |corrector|
- replacement = "change(#{receiver}, :#{message})"
+ replacement = "change(#{receiver.source}, :#{message})"
corrector.replace(node, replacement)
end
end A completely different thing is More on this #814 rspec/rspec-expectations#1139 rspec/rspec-expectations#1125 https://blog.rubystyle.guide/rspec/2019/07/17/rspec-implicit-block-syntax.html There's a dedicated cop,
Would you like to tackle those issues, @YusukeIwaki ? |
Would you like to turn the changes from the above comment into a pull request and throw in a couple of tests, @YusukeIwaki ? |
@pirj Thank you for your kind advice, I will try it. |
We use
RSpec/ExpectChange
withEnforcedStyle: block
.This rule works well with not nested class like below:
But when we use nested class like
User::Token
instead ofToken
, this rule doesn't work.It can be easily reproduced with spec/rubocop/cop/rspec/expect_change_spec.rb.
The text was updated successfully, but these errors were encountered: