-
-
Notifications
You must be signed in to change notification settings - Fork 281
Recognize stubbing of described_class
in RSpec/SubjectStub
#2088
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
Open
lovro-bikic
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
lovro-bikic:subject-stub-described-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, this is useful. Why is this bad?
how is this different from
allow(Article)
?I see that in a spec dedicated to Article it may be strange to mock its methods, specifically the initializer, or other static ones.
However, how do you otherwise test static methods that call other static methods in isolation? (Considering those static methods arr the subject under test, noth the whole class).
Can you probably give a more realistic example of bad code that made you extend this cop in the forst place?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same as
allow(Article)
, but note that there's another cop RSpec/DescribedClass which would autocorrectArticle
todescribed_class
.In fact, I think
allow(Article)
should be registered as an offense as well since it's also the subject under test (because not everyone might have RSpec/DescribedClass enabled). I can work on this as a separate PR.Let's say your subject is
subject(:article) { Article.new }
. This cop will currently register this:as an offense.
However, it won't register:
as an offense, which is quite similar to the above (except the former will stub only one instance, and the latter will stub any instance).
In both scenarios, we are stubbing a method of the object under test.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's say we have a simple module:
and a spec like:
In my view, this is not a good spec because we're not letting the module execute the code we're supposedly testing. A better spec would be:
which goes through the whole code path of the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s an easy one.
be_a(String)
won’t cut it - if someone mistakenly replaces the call touuid
with a static"not really random"
, the test won’t catch it. You have to stub, but not the private method, but the externaluuid
.Stubbing the class under test’s public methods though may be an indication of a smell or may not. I don’t feel we should always flag corresponding tests as offenses.
Do you have other examples, preferably real life ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a real-life example 😄
If
TokenGenerator
were a class instead, this cop would register an offense:Can you explain to me why should this scenario be an offense under this cop, but the
module TokenGenerator
example shouldn't?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough!
But is it still the same "subject under test" if we make assertions on an instsnce, but stub class methods? 🤔
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. With this PR and also #2094 I've expanded the cop to register offenses for not just the subject's methods (which are typically an instance's methods), but also any method of the class/module for any instance. I guess this gives more responsibility to this cop, which might or might not be desirable.
The style guide shows this:
as a good example, but the test could still stub the
#author
method to returnnil
in any of the following ways:and these would not be offenses under any RSpec cop, even though they are in practice similar to:
which is an offense.
Personally, I think
(allow|expect)_any_instance_of
anddescribed_class
should register offenses somewhere, much likeRSpec/SubjectStub
does, but perhaps this should be a different cop (e.g.RSpec/DescribedClassStub
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your argument related to the usage of
*_any_instance_of
is convincing.It’s tougher with
described_class
. I especially like this comment.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True (I also got bit by this when I once used
describe :symbol
foolishly), but then again, there's theRSpec/DescribedClass
cop which enforces usage ofdescribed_class
, including for stubs. Couldn't offenses for stubbingdescribed_class
follow the same logic as that cop?