Skip to content
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

double/instance_double argument type #1136

Open
bquorning opened this issue Mar 23, 2021 · 5 comments
Open

double/instance_double argument type #1136

bquorning opened this issue Mar 23, 2021 · 5 comments

Comments

@bquorning
Copy link
Collaborator

I'm seeing double/instance_double/class_double/object_double/spy etc. being called with sometimes a string, sometimes a symbol (perhaps because the rspec-mocks documentation explicitly allows both), and of course sometimes a constant referring to a class or module.

I think mixing string and symbol arguments is a bit of a mess, so I would propose making a new cop that enforces either one or the other – with strings being the default enforced style. Why? Because "MyClass" looks better than :MyClass in those cases where you pass in a module name as a string/symbol.

@pirj
Copy link
Member

pirj commented Mar 23, 2021

instance_double accepts a class, and so does class_double/instance_spy. I see no good reasons not to use this. Except to avoid excessive class loading when instance_double and the class is referenced from globally-defined shared examples, and those shared examples are eagerly loaded from spec helper.

@pirj
Copy link
Member

pirj commented Oct 23, 2022

I can also add that changing from a class literal to a string/symbol arguably makes the cop unsafe unless verify_doubled_constant_names is turned on. It is off by default in RSpec Mocks 3.11 and the upcoming 4.0, too for the reasons I struggle to remember. Related PR turning on verify_partial_doubles by default.

@corsonknowles
Copy link
Contributor

We have a very simple Rubocop rule that autocorrects from string to Class for instance_double. It does not affect symbol declarations. Is there interest in us open-sourcing this? I'd be happy to push it up.

@pirj
Copy link
Member

pirj commented Sep 17, 2024

That will be very kind of you, @corsonknowles ! A pr is welcome.

corsonknowles added a commit to corsonknowles/rubocop-rspec that referenced this issue Sep 17, 2024
corsonknowles added a commit to corsonknowles/rubocop-rspec that referenced this issue Sep 17, 2024
@corsonknowles
Copy link
Contributor

corsonknowles commented Sep 17, 2024

Happy to help! Here's a draft PR:

We've run this on over 25,000 files and had it in place for a few years.

I'm running into a little hiccup with the rake task for documentation (but the ardocs did successfully generate)

corsonknowles added a commit to corsonknowles/rubocop-rspec that referenced this issue Sep 17, 2024
corsonknowles added a commit to corsonknowles/rubocop-rspec that referenced this issue Sep 18, 2024
Addresses rubocop#1136

Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.
corsonknowles added a commit to corsonknowles/rubocop-rspec that referenced this issue Sep 18, 2024
Addresses rubocop#1136

Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants