1

I use the geocoder gem in my app, and when I run my tests, I meet the limit query error from Google. So I don't want to use it when I'm in a test environment, and I want some of my pages not to display some information when I'm in test, in order not to use the geocoder utility.

I tried this but it didn't worked, can you help me ?

app/models/location.rb

after_validation :geocode, if: :address_changed? && (Rails.env.production? || Rails.env.development? )

And

app/helpers/location.rb used in the app/views/products/index.html.erb

if Rails.env.test?
  t('location.distance.test')
else
  here.location.distance_to(there.location, :km)
end

That got me the following error message for every test I run :

 NoMethodError:
   undefined method `after_validation' for false:FalseClass

And if I remove everything in my helper and the following code in my model, I have no more error.

&& (Rails.env.production? || Rails.env.development? )
Flo Rahl
  • 1,044
  • 1
  • 16
  • 33
  • In what way did it not work? Did you get an error or was the check ignored? – Shadwell Aug 29 '13 at 09:10
  • I got an error, because the method was unknown (I edited my post accordingly). – Flo Rahl Sep 05 '13 at 06:40
  • Flo, could you please post the full code that is throwing the error: -> the model (you can of course remove the methods not related to this issue) -> the method actually throwing the error: is it when you are trying to save a new instance ? Thanks – NicoArbogast Sep 05 '13 at 10:03
  • Nico, I'm sure that's my syntax that is incorrect, because when I remove the checkings `Rails.env.production?` and `Rails.env.development?`, it works just fine. – Flo Rahl Sep 05 '13 at 10:18

1 Answers1

2

Write it like that:

unless Rails.env.test?
  after_validation :geocode, if: :address_changed?
end
Damien
  • 26,933
  • 7
  • 39
  • 40