Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec: undefined method 'double'

I'm trying to create a double but I keep getting this error:

 undefined method `double' for #<Class:0x007fa48c234320> (NoMethodError)

I suspect the problem has got to do with my spec helper so I'm adding my spec helper below:

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'rspec'
require 'webmock/rspec'
include WebMock::API
include WebMock::Matchers

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
end
like image 369
tommi Avatar asked Jun 24 '12 17:06

tommi


2 Answers

double exists inside examples (and before blocks etc) but it sounds like you're trying to call it outside one of those contexts.

So for example

describe Thing do
  thing = double()
  it 'should go bong'
  end
end

is incorrect but

describe Thing do
  before(:each) do
    @thing = double()
  end

  it 'should go bong' do
    other_thing = double()
  end
end

is fine

like image 186
Frederick Cheung Avatar answered Nov 16 '22 19:11

Frederick Cheung


You can also use RSpec's double outside of RSpec by requiring the standalone file.

require "rspec/mocks/standalone"

greeter = double("greeter")
allow(greeter).to receive(:say_hi) { "Hello!" }
puts greeter.say_hi

From the docs: https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/outside-rspec/standalone

like image 9
robert_murray Avatar answered Nov 16 '22 20:11

robert_murray