Ruby 如何在rspec之外使用rspec期望值和匹配器?
我有一个脚本,它已经演变成需要做一些断言和匹配 它是用ruby编写的,我在GEM文件中包含了Ruby 如何在rspec之外使用rspec期望值和匹配器?,ruby,rspec,rspec-expectations,Ruby,Rspec,Rspec Expectations,我有一个脚本,它已经演变成需要做一些断言和匹配 它是用ruby编写的,我在GEM文件中包含了rspec,并且需要它 我觉得这很有帮助,所以在irb中如何使用这篇文章: 我还发现: 我在expect行得到一个错误。当包含一个模块时,它使其方法可用于类的实例。您的test方法是单例方法(“类方法”),而不是实例方法,因此永远无法访问混合模块提供的方法。要修复它,您可以执行以下操作: class BF include ::RSpec::Matchers def test e
rspec
,并且需要它
我觉得这很有帮助,所以在irb
中如何使用这篇文章:
我还发现:
我在
expect
行得到一个错误。当包含一个模块时,它使其方法可用于类的实例。您的test
方法是单例方法(“类方法”),而不是实例方法,因此永远无法访问混合模块提供的方法。要修复它,您可以执行以下操作:
class BF
include ::RSpec::Matchers
def test
expect(1).to eq(1)
end
end
BF.new.test
如果希望RSpec::Matchers
方法可用于BF
的单例方法,则可以扩展模块:
class BF
extend ::RSpec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test
... 你得到了什么错误?啊,是扩展看起来像我想要它做的!现在就试!
class BF
extend ::RSpec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test