Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method instance_double for RSpec::Mocks::ExampleMethods

I have a test case like this:

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

When I run RSpec, I get an error:

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

According to http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods this method exists. So I tried to access it directly by:

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

And then I got a very surprising error:

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

which is contrary to the documentation I linked above.

What am I missing?

like image 381
Jacek Avatar asked Mar 04 '14 17:03

Jacek


1 Answers

From the documentation you pointed to:

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

Try to include it into your code:

include RSpec::Mocks::ExampleMethods

Your direct approach failed, because calling

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

expects that the method was declared as a class method:

def self.instance_double(...)

but it has been declared as an instance method :

def instance_double(...)
like image 68
Uri Agassi Avatar answered Oct 23 '22 01:10

Uri Agassi