【问题标题】:rspec: undefined method 'double'rspec:未定义的方法“双”
【发布时间】:2012-06-24 17:40:30
【问题描述】:

我正在尝试创建双精度,但我不断收到此错误:

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

我怀疑这个问题与我的规范助手有关,所以我在下面添加了我的规范助手:

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'rspec'
require 'webmock/rspec'
include WebMock::API
include WebMock::Matchers

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
end

【问题讨论】:

  • 你在哪里/怎么称呼双倍?
  • 在我描述 block 之前的 it 块。
  • double 存在于示例中(以及 before() 块等),但听起来您正试图在其中一个上下文之外调用它。
  • @FrederickCheung 现在可以使用了。谢谢!我会去阅读以加强一些理解。
  • @FrederickCheung:也许你可以从你的评论中做出回答,这样这个问题就不会悬而未决。

标签: ruby rspec rubygems rspec2


【解决方案1】:

double 存在于示例中(以及 before 块等),但听起来您正试图在其中一个上下文之外调用它。

例如

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

没问题

【讨论】:

    【解决方案2】:

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

    require "rspec/mocks/standalone"
    
    greeter = double("greeter")
    allow(greeter).to receive(:say_hi) { "Hello!" }
    puts greeter.say_hi
    

    来自文档:https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/outside-rspec/standalone

    【讨论】:

    • 这应该是公认的答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多