Undefined instance_double method for RSpec :: Mocks :: ExampleMethods

I have a test case:

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

When I start RSpec, I get an error message:

 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:

 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 amazing error:

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

which contradicts the documentation that I linked above.

What am I missing?

+6
source share
1 answer

From the documentation you pointed to:

Mix this in your test context (for example, the base class of the test environment) to use rspec-mocks with your test infrastructure.

Try include in your code:

 include RSpec::Mocks::ExampleMethods 

Your direct approach failed because the challenge

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

expects a method to be declared as a class method:

 def self.instance_double(...) 

but it was declared as an instance method:

 def instance_double(...) 
+1
source

All Articles