سؤال

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? )
هل كانت مفيدة؟

المحلول

Write it like that:

unless Rails.env.test?
  after_validation :geocode, if: :address_changed?
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top