Ruby rspec:未定义的方法&x27;双倍';

Ruby rspec:未定义的方法&x27;双倍';,ruby,rspec,rubygems,rspec2,Ruby,Rspec,Rubygems,Rspec2,我试图创建一个double,但不断出现以下错误: undefined method `double' for #<Class:0x007fa48c234320> (NoMethodError) double存在于示例中(以及之前的块等),但听起来您试图在这些上下文之外调用它 比如说 describe Thing do thing = double() it 'should go bong' end end 是不正确的,但是 describe Thing do b

我试图创建一个double,但不断出现以下错误:

 undefined method `double' for #<Class:0x007fa48c234320> (NoMethodError)

double
存在于示例中(以及
之前的
块等),但听起来您试图在这些上下文之外调用它

比如说

describe Thing do
  thing = double()
  it 'should go bong'
  end
end
是不正确的,但是

describe Thing do
  before(:each) do
    @thing = double()
  end

  it 'should go bong' do
    other_thing = double()
  end
end

很好

您也可以通过要求独立文件在RSpec之外使用RSpec的

require "rspec/mocks/standalone"

greeter = double("greeter")
allow(greeter).to receive(:say_hi) { "Hello!" }
puts greeter.say_hi

从文档中:

您在哪里/如何调用double?在我的描述
块中
之前的
块中。double存在于示例中(和之前的()块等),但听起来好像您试图在这些上下文之外调用它。@Frederickhueng它现在可以工作了。谢谢“我要去读一读,以重新获得一些理解”。@弗雷德里克秋:也许你可以从你的信中作出回答,这样这个问题就不会没有答案了。这应该是公认的答案。谢谢
require "rspec/mocks/standalone"

greeter = double("greeter")
allow(greeter).to receive(:say_hi) { "Hello!" }
puts greeter.say_hi