加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 用于RSpec的未定义方法instance_double :: Moc

发布时间:2020-12-16 19:05:32 所属栏目:百科 来源:网络整理
导读:我有一个这样的测试用例: describe WorkCardsController do it "something" do work_card = instance_double(WorkCard,{:started?=true} ) #some more code endend 当我运行RSpec时,我收到一个错误: undefined method 'instance_double' for #Rspec::Core:
我有一个这样的测试用例:
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

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

我错过了什么?

解决方法

从您指出的文档:

Mix this in to your test context (such as a test framework base class) to use rspec-mocks with your test framework.

尝试将其包含在您的代码中:

include RSpec::Mocks::ExampleMethods

你的直接接近失败,因为打电话

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

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

def self.instance_double(...)

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

def instance_double(...)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读