gpt4 book ai didi

ruby-on-rails - RSpec::Mocks::ExampleMethods 的未定义方法 instance_double

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:51 29 4
gpt4 key购买 nike

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

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(...)

关于ruby-on-rails - RSpec::Mocks::ExampleMethods 的未定义方法 instance_double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178416/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com