Andrew Grimm
Andrew Grimm

Reputation: 81610

Specify that an object doesn't receive any messages in RSpec

I know how to specify that an object shouldn't receive a specific message:

expect(File).to_not receive(:delete)

How would I specify that it shouldn't receive any messages at all? Something like

expect(File).to_not receive_any_message

Upvotes: 27

Views: 16351

Answers (2)

Frederick Cheung
Frederick Cheung

Reputation: 84162

Sounds like you just want to replace the object in question with a double on which you have defined no expectations (so any method call would result in an error). In your exact case you could do

stub_const("File", double())

Upvotes: 22

SHS
SHS

Reputation: 7744

I'm not sure what the use-case would be. But the following is the only direct answer I can come up with:

methods_list = File.public_methods # using 'public_methods' for clarity
methods_list.each do |method_name|
  expect(File).to_not receive(method_name)
end

In case you want to cover all methods (i.e. not just the public ones):

# readers, let me know if there is a single method to
# fetch all public, protected, and private methods
methods_list = File.public_methods +
               File.protected_methods +
               File.private_methods

Upvotes: 4

Related Questions