我有一个脚本已经演变成需要做一些断言和匹配。
它是用 ruby 编写的,我已经包含rspec
在 Gemfile 中并需要它。
我发现这篇关于如何使用的非常有用的 SO 帖子irb
:
我还发现了以下内容:
class BF
include ::Rspec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test
我在线路上遇到错误expect
。
我有一个脚本已经演变成需要做一些断言和匹配。
它是用 ruby 编写的,我已经包含rspec
在 Gemfile 中并需要它。
我发现这篇关于如何使用的非常有用的 SO 帖子irb
:
我还发现了以下内容:
class BF
include ::Rspec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test
我在线路上遇到错误expect
。
当你include
是一个模块时,它使它的方法可用于类的实例。您的test
方法是单例方法(“类方法”),而不是实例方法,因此永远无法访问混合模块提供的方法。要修复它,您可以执行以下操作:
class BF
include ::RSpec::Matchers
def test
expect(1).to eq(1)
end
end
BF.new.test
如果您希望这些RSpec::Matchers
方法可用于 的单例方法BF
,则可以使用extend
以下模块:
class BF
extend ::RSpec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test