Reputation: 6209
In rspec, you can use an instance_double to ensure that your expectations are valid - in other words, if I have:
class Foo
def bar
end
end
I can do
dbl = instance_double('Foo')
expect(dbl).to receive(:bar)
...
and I know that if I remove the bar
method from Foo
, my test will throw an error, preventing me from silently breaking things.
How can I achieve this same effect with class methods? I just accidentally caused a bug which my tests didn't catch, because I did the following:
class Foo
def self.bar
end
end
Test:
expect(Foo).to receive(:baz)
Foo.baz
No errors were raised because I added a stub via the above expectation, but really that method doesn't exist. I'm aware I could do class_double
, but Foo.baz
is called in another part of my code, not in my test, so I can't get it to call the class_double
instead of the actual class.
Upvotes: 1
Views: 1062
Reputation: 84182
What you want to do is turn on partial double verification:
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
This will cause partial doubles to have the same behaviour as doubles created via object_double
.
Upvotes: 1