指定对象在 RSpec 中不接收任何消息

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

我知道如何指定对象不应接收特定消息:

expect(File).to_not receive(:delete)

我如何指定它根本不应该接收任何消息?像

expect(File).to_not receive_any_message

我不确定用例是什么。但以下是我能想出的唯一直接答案:

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

如果您想涵盖所有方法(即不仅仅是 public 方法):

# 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

听起来您只是想将有问题的对象替换为您没有定义期望值的双精度对象(因此任何方法调用都会导致错误)。在你的确切情况下你可以做

stub_const("File", double())