【问题标题】:undefined method instance_double for RSpec::Mocks::ExampleMethodsRSpec::Mocks::ExampleMethods 的未定义方法 instance_double
【发布时间】:2014-03-04 17:09:21
【问题描述】:

我有一个这样的测试用例:

describe WorkCardsController do
    it "something" do
        work_card = instance_double(WorkCard, {:started?=>true} )
        #some more code
    end
end

当我运行 RSpec 时,我得到一个错误:

undefined method 'instance_double' for #<Rspec::Core::ExampleGroup::Nested_1::Nested_8::Nested_3:0x007f0788b98778>

根据http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods这个方法是存在的。所以我尝试通过以下方式直接访问它:

describe WorkCardsController do
    it "something" do
        work_card = RSpec::Mocks::ExampleMethods::instance_double(WorkCard, {:started?=>true} )
        #some more code
    end
end

然后我得到了一个非常令人惊讶的错误:

undefined method 'instance_double' for Rspec::Mocks::ExampleMEthods:Module

这与我上面链接的文档相反。

我错过了什么?

【问题讨论】:

  • 你确定有rspec3吗?现在的gem版本是2.14,所以如果你没有通过github安装,那方法不存在也是正常的。

标签: ruby-on-rails ruby unit-testing rspec rspec-mocks


【解决方案1】:

从您指向的文档中:

将其混合到您的测试上下文(例如测试框架基类)中,以便在您的测试框架中使用 rspec-mocks。

尝试将include 输入到您的代码中:

include RSpec::Mocks::ExampleMethods

你的直接方法失败了,因为调用

RSpec::Mocks::ExampleMethods::instance_double(...)

期望该方法被声明为类方法:

def self.instance_double(...)

但它已被声明为实例方法:

def instance_double(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多