-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Complete branch coverage for more files #1990
Complete branch coverage for more files #1990
Conversation
2ff8b66
to
1dedfd0
Compare
Surprisingly little duplication between this and #1989 :yay: |
1dedfd0
to
331a822
Compare
331a822
to
88b07e7
Compare
@bquorning @ydah I reconciled this with #1989 and it should be ready to go! |
:expect_any_instance_of)).to eq(:allow_any_instance_of) | ||
end | ||
|
||
it 'falls through silently and returns nil for unknown methods' do |
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.
technically this is all that's needed in here to solve for the case
statement branch coverage
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.
I don’t particularly like this kind of spec. Testing a private method with very edge case input doesn’t provide a whole lot of value. Actually we could change StubbedMock#replacement
to do a hash lookup, and SimpleCov would claim 100% coverage:
def replacement(method_name)
{
expect: 'allow',
is_expected: 'allow(subject)',
expect_any_instance_of: 'allow_any_instance_of'
}.fetch(method_name)
end
I think it would be better look at whether there might actually be a problem with the method_name
value which is passed into #replacement
. It seems to be found in #on_send
by the expectation
node matcher, which is looking for #Expectations.all
… That method is define in language.rb as returning the language configuration for Expectations, which in default.yml is set to this array of values:
- are_expected
- expect
- expect_any_instance_of
- is_expected
- should
- should_not
- should_not_receive
- should_receive
I’m thinking, could we make test case (using expect_offense
or expect_no_offenses
) using one of those other values, and thereby hit the code that’s currently not covered by tests?
aadca81
to
679c0f8
Compare
The Ruby head CI failure comes down to how rocket hashes are spaced in the parser. |
See rubocop-rspec/spec/rubocop/rspec/hook_spec.rb Lines 77 to 83 in 00b6c03
|
664b1f5
to
d252842
Compare
expect(bar).to have_received(:baz) | ||
end | ||
RUBY | ||
end |
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 test case is good 👍🏼
I actually found again that the lib/ code can be optimized a bit changes a bit, moving code into the node pattern:
diff --git a/lib/rubocop/cop/rspec/instance_spy.rb b/lib/rubocop/cop/rspec/instance_spy.rb
index cca5a1e6..43298d35 100644
--- a/lib/rubocop/cop/rspec/instance_spy.rb
+++ b/lib/rubocop/cop/rspec/instance_spy.rb
@@ -36,7 +36,7 @@ module RuboCop
def_node_search :have_received_usage, <<~PATTERN
(send
(send nil? :expect
- (lvar $_)) :to
+ (lvar %1)) :to
(send nil? :have_received
...)
...)
@@ -46,9 +46,7 @@ module RuboCop
return unless example?(node)
null_double(node) do |var, receiver|
- have_received_usage(node) do |expected|
- next if expected != var
-
+ have_received_usage(node, var) do
add_offense(receiver) do |corrector|
autocorrect(corrector, receiver)
end
It would be nice to find a way to calculate test coverage of the node patterns…
:expect_any_instance_of)).to eq(:allow_any_instance_of) | ||
end | ||
|
||
it 'falls through silently and returns nil for unknown methods' do |
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.
I don’t particularly like this kind of spec. Testing a private method with very edge case input doesn’t provide a whole lot of value. Actually we could change StubbedMock#replacement
to do a hash lookup, and SimpleCov would claim 100% coverage:
def replacement(method_name)
{
expect: 'allow',
is_expected: 'allow(subject)',
expect_any_instance_of: 'allow_any_instance_of'
}.fetch(method_name)
end
I think it would be better look at whether there might actually be a problem with the method_name
value which is passed into #replacement
. It seems to be found in #on_send
by the expectation
node matcher, which is looking for #Expectations.all
… That method is define in language.rb as returning the language configuration for Expectations, which in default.yml is set to this array of values:
- are_expected
- expect
- expect_any_instance_of
- is_expected
- should
- should_not
- should_not_receive
- should_receive
I’m thinking, could we make test case (using expect_offense
or expect_no_offenses
) using one of those other values, and thereby hit the code that’s currently not covered by tests?
@@ -64,4 +64,29 @@ | |||
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Baz | |||
YAML | |||
end | |||
|
|||
describe '#unified_config' do | |||
context 'when cop is in SUBDEPARTMENTS or AMENDMENTS' do |
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.
First up, I think that since 1fa9ce3 is long merged, we can just remove SUBDEPARTMENTS
now.
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.
And second, it looks like AMENDMENTS
is currently a String and not an Array. Which is… confusing, to put it mildly. Aren’t we essentially doing this:
"abcde".gsub(*%(bc), 'x')
🙈
@@ -45,7 +45,7 @@ def metadata | |||
private | |||
|
|||
def valid_scope?(node) | |||
node&.sym_type? && Language::HookScopes.all(node.value) | |||
node.sym_type? && Language::HookScopes.all(node.value) |
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.
👍🏼
d252842
to
9ca0da8
Compare
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.
Thanks
Increase branch coverage minimum threshold.
Modernize
.rubocop_todo.yml
with Rubocop 1.6.8In the further pursuit of full coverage for
Rubocop/RSpec
, this solves for additional files.Note
We remove a safe operator from a method used in a node matcher here, since it should always get a node.
Reconciled with:
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded: "<<next>>"
indefault/config.yml
.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"
inconfig/default.yml
.