-
-
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/SubjectStub matches all matcher #488
Comments
I don't get your point, You are still stubbing the part of the subject. Maybe you can help with a more real-world example of this? |
The real-world example is in https://github.com/bbatsov/rubocop/pull/4989/files#diff-be297a2e1f10d4ed00f8faaf78f0f06c |
10x. Now it makes perfect sense |
So there is an object that acts as a container for and a delegator of method calls to containing items by inheriting from an The original code that suffered from a false detection was:
However, it has been reworked to:
which in case of this specific object design is more indicative of what happens. @bquorning @Darhazer WDYT of dropping the special case for |
You’re linking to the wrong repository. This is RuboCop master: https://github.com/rubocop-hq/rubocop/blob/70ae6ca7978770ea699a2809f57ca2a6bdeec6be/spec/rubocop/formatter/formatter_set_spec.rb#L27 |
You're right, I followed the wrong link, sorry for the confusion. Anyway, the current way to test it is a bit odd:
How is it better than the following?
|
triggers |
Since there are only two items on that list, it could be
As far as I can see, |
The only thing keeping from approving #770 is this issue. So, we have an @pirj, in your last comment you say we can just write out each expectation,
What if there were ten or twenty items on the list? I wouldn’t write 20 lines with identical expectations. But would using the |
@bquorning good question. There are two of them in the spec to, I assume, make sure that more than one formatter can be added to the set. Covering the case with more formatters is a slippery path since there's no limitation on the number of formatters. It's possible to interpret "Needed data" recommendation in a way that one shouldn't test all possible cases, just enough of them. Let's look at this spec from a different angle. There are formatters, and there is a container of formatters. We chose the container to be a subject of this spec. In this example, we send some message to the container and expect formatters to receive the same message. A typical spec would look like this: subject(:formatter_set) { FormatterSet.new(formatters) }
let(:formatters) { 2.times { instance_double(Formatter) } } # or spy(Formatter)
it 'invokes the same method of all containing formatters' do
files = double
expect(formatters).to all receive(:started).with(files)
formatter_set.started(files)
end which will be ignored by both Unfortunately, due to the current Well, I would agree if you said that usage of expect(formatter_set[0]).to receive(...) is not any better than expect(formatter_set).to all receive(...) and I would partially agree with that. Both cases are kind of odd. But this doesn't mean the cop in question should tolerate the case with |
So I tend to agree with @pirj line of reasoning and if you really need to test the delegation in the collection while not having control of what collection actually contains, you can always disable the cop for this spec, or use |
Agreed. |
Here is a patch to reproduce problem.
In above case, subject is array, and it just stub item of subject. So it should not add offence, I think.
The text was updated successfully, but these errors were encountered: