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?

1 Answer 1

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
3
  • I added it to the bottom of my spec_helper file but I'm getting an undefined method stub error. Any ideas? Aug 8, 2012 at 15:20
  • 1
    In that case add it to to the config block. See my updated answer. Aug 8, 2012 at 15:22
  • That's pretty awesome! Thanks Aug 8, 2012 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.