Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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
like image 310
Andrew Grimm Avatar asked Apr 09 '15 05:04

Andrew Grimm


2 Answers

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())
like image 55
Frederick Cheung Avatar answered Nov 13 '22 05:11

Frederick Cheung


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
like image 25
SHS Avatar answered Nov 13 '22 05:11

SHS