Ruby on rails 如何使用Rspec检查方法是否存在?

Ruby on rails 如何使用Rspec检查方法是否存在?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我想写一个清晰的检查方法是否存在: expect(subscriber.respond_to?(:fake_method)).to be(true) <-- This fails (as expected) expect(subscriber).respond_to?(:fake_method) <-- This passes, why? expect(订阅者。回复?(:fake_方法))。to be(真实)我相信我有答案。第二个约定不起作用,

我想写一个清晰的检查方法是否存在:

expect(subscriber.respond_to?(:fake_method)).to be(true)    <-- This fails (as expected)

expect(subscriber).respond_to?(:fake_method)                <-- This passes, why?

expect(订阅者。回复?(:fake_方法))。to be(真实)我相信我有答案。第二个约定不起作用,因为根据文档,匹配器不同:

您应该尝试以下方法:

expect(subscriber).to respond_to(:fake_method) 

干杯

我们可以简单地使用
expect(subscriber)。不响应(:fake\u method)
用于否定的情况。

谢谢@Kryptman,但我得到了一个错误:
ArgumentError:expect语法不支持运算符匹配器,因此您必须将匹配器传递给“#to”。
Doh!您是对的,但需要删除问号,您的链接确实有帮助:
expect(订户)。要响应(:fake\u方法)
Updated!我很高兴这很有用马克:)