apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.26k stars 382 forks source link

Possible to turn off validation for testing #44

Closed sshaw closed 13 years ago

sshaw commented 13 years ago

Hello,

Since model.gmaps4rails_options returns a new hash each time it's called one cannot modifying an including instance's options. This makes sense though, as most of the options shouldn't be modified, but how can one easily run their unit tests without using real addresses or installing a mock lib.

Factory.define :event do |e|
  # ...
  e.after_build do |ev|   
     ev.gmaps4rails_options[:validation] = false if ev.respond_to? :gmaps4rails_options
  end
end

Instead I've had to do the more invasive: acts_as_gmappable :validation => !Rails.env.test? Maybe there's a better way?

apneadiving commented 13 years ago

Hi,

for test purposes, I do use class_eval, see here: https://github.com/apneadiving/Google-Maps-for-Rails/blob/master/test/dummy/spec/models/user_spec.rb

I suggest the following:

  e.after_build do |ev|
     ev.class.class_eval do
        def gmaps4rails_options
          #redefine whole hash here
         end
      end
  end
sshaw commented 13 years ago

Yes, I think that's a better route.

Slightly modified :

ev.instance_eval do 
  @__gmaps4rails_options = gmaps4rails_options.merge(:validation => false)
  def gmaps4rails_options
    @__gmaps4rails_options
  end
end

Thanks!