Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在rspec之外使用rspec期望值和匹配器?_Ruby_Rspec_Rspec Expectations - Fatal编程技术网

Ruby 如何在rspec之外使用rspec期望值和匹配器?

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

我有一个脚本,它已经演变成需要做一些断言和匹配

它是用ruby编写的,我在GEM文件中包含了
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