rubocop-rspec
rubocop-rspec copied to clipboard
More general cop for stubbing system under test
Checking that the subject isn't being stubbed is good, but typically in unit testing you want to be sure not to stub the class that you're testing. Provided that the initial describe block is written correctly, I wonder if it would be easy to catch these:
Bad:
describe Foo do
before do
allow(Foo).to receive(:bars).and_return([:baz, :qux])
end
end
Also bad:
describe Foo do
let(:foo) { Foo.new }
before do
allow(foo).to receive(:qux?).and_return(true)
end
end
Good idea. Might make sense as a separate cop since it sounds like you are talking about more than just detecting the subject being stubbed. StubbedDescribedClass maybe?
How to deal with this, if the described class is quoted as RSpec core team recommends (also related):
RSpec.describe 'Foo' do
before do
allow(Foo).to receive(:bars).and_return([:baz, :qux])
end
end
before's block will be called when the spec is executed, and the class is already loaded, it's something that is being used as a lazy loading measure.
It also gets more complicated with modules involved:
module A
RSpec.describe B do
before { allow(B).to receive( ... ) }
end
end
Can't tell of the top of my head, but testing of templated modules/classes can be affected as well.