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/DescribedClass mis-identifies inside of rspec-rails anonymous controllers #795
Comments
Thanks for reporting, I'll take a look. |
It works fine for some blocks, so I'd rather not make any code changes: class A
def self.described_class
'A'
end
def self.y(&block)
block.call
end
y do
puts self # => A
puts described_class # => A
end
def self.x(&block)
Class.new(&block)
end
x do
puts self # => #<Class:0x00...
puts described_class # fails
end
end There's no way to statically determine what kind of block it is. It's a known caveat, and there's a configuration option to work this around, RSpec/DescribedClass:
SkipBlocks: true I suggest you add a Please let me know if it works for you, @benhutton, @anthony-robin. |
Thanks @pirj ! The |
@benhutton Did adding a directory-local RuboCop configuration file solve the issue you're experiencing? |
@pirj right now, I'm preferring to use a single-line |
@benhutton Unfortunately, there's no other way to set configuration options per-directory right now. You mean that situation when in a controller test there a non- Do you think it is worth it? Speaking of |
@pirj I'm not talking about configuring per-directory. I'd do it globally. That discussion is interesting. And I think this adds an interesting detail: you can't even actually use That said, I don't do the things that make it hard to use. It always works for me (except in these controller specs!). So I would probably keep it around. Maybe with a For this particular cop, it feels really tricky, right? The cop cannot do what it purports to do reliably. So it's unsafe. So maybe I would disable the whole cop by default and then go with the |
The mention of As far as I know, it's a common way to configure cops in directories. There is a TODO to mark this cop as "Unsafe to autocorrect" on cop metadata roadmap. Is there anything that can be done in the scope of this ticket, @benhutton ? |
@pirj from what I can tell, the caveat is in the cop's specs, not in the actual docs (https://rubocop-rspec.readthedocs.io/en/latest/cops_rspec/#rspecdescribedclass). Thus, it's a known and tested-for caveat, but not one that is well-communicated to users. So I'd say that the safest thing to do would be to enhance the docs clarifying why the skip blocks option exists and when/how to use it? |
Fair enough. I was completely sure that it was in the docs, I've definitely seen it somewhere, my bad, and please accept my apologies. Please take a look #802, @benhutton. Do you think this would be enough to avoid confusion? Just for history: #59 |
yes! that is great! thanks so much @pirj |
Versions:
rubocop
— 0.74.0rubocop-rspec
— 1.34.1RSpec/DescribedClass will correct code like this:
to this:
Resulting in this error:
For reference, please see https://relishapp.com/rspec/rspec-rails/v/3-8/docs/controller-specs/anonymous-controller
The text was updated successfully, but these errors were encountered: