16

I'm wanting to know if there is an easy way to include a stub in all of my spec files. I'm using the Geocoder gem and when I run my RSpec tests I don't want it attempting to download the location information.

I've found the following solution which works perfectly. However, I don't want to write the same three lines in every spec file.

before(:each) do
  User.any_instance.stub(:geocode) { [1,1] }
end

Is it possible to put something in my spec_helper.rb file?

Community
  • 1
  • 1
Baylor Rae'
  • 3,995
  • 20
  • 39

1 Answers1

28

You can put it in the spec_helper instead, like this:

RSpec.configure do |config|
  config.before(:each) do
    User.any_instance.stub(:geocode) { [1,1] }
  end
end
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41