8

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?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Jacek
  • 81
  • 1
  • 4
  • Are you sure to have rspec3? The gem version in this moment is 2.14, hence if you haven't installed it by github, it's just normal that method doesn't exists. – Iazel May 11 '14 at 17:42

1 Answers1

3

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(...)
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93