dgollahon/rspectre

Check for unused stubs

Opened this issue · 3 comments

Today, I used rspectre to remove 250 unused lines from our spec suite. So: Thank you!

Quite a few of these removed lets were actually still being referenced in blocks in unused stubs, such as:

let(:baz) { "never used" }
before do
  allow(foo).to receive(:bar) { baz } # foo.bar is never called.
end

Would it be possible for rspectre to also check for unused stubs, or it that a job for another tool?

Today, I used rspectre to remove 250 unused lines from our spec suite. So: Thank you!

Awesome! Glad it was helpful. :D

Would it be possible for rspectre to also check for unused stubs, or it that a job for another tool?

Yeah, I think that's actually a really good idea. I think it'd be fairly manageable to support in at least a few cases if not all. The tricky part is that stubbing is chainable so it might be hard to determine if the whole expression is useless, but I imagine I can figure something out.

On a related note, I've always planned to handle this for doubles--it's common to make the same mistake there where you do something like instance_double(Foo, bar: unused_baz). I think I will probably start with fixing the double problem first, but I will definitely look into stubbing situations like you have here.

I think this is now roughly three categories of features I want:

  • Remove unused doubled methods from spy, instance_spy, double, instance_double, object_double, etc.
  • Remove unused stubs from expect, allow, receive_message_chain, etc.
  • Check for unecessary stub_const (see rubocop/rubocop-rspec#423 (comment))

I may break this into other issues later, but I think this suffices for now.

Remove unused stubs from expect, allow, receive_message_chain, etc.

This issue now just tracks this--I split out the other two.