Reputation: 1021
I am using ActiveSupport::TimeZone to set the time zone on a location based on the zip code.
def set_time_zone
self.time_zone = ActiveSupport::TimeZone.find_by_zipcode(self.zip)
end
This works just fine in the application itself. I am calling the set_time_zone on before_save.
In running the tests with Rspec, when it tries to run the set_time_zone method it errors out with "undefined method 'find_by_zipcode'in ActiveSupport::TimeZone"
I have included "require 'active_support/time_with_zone'" in my spec helper as well.
For now my work around is excluding the before save if in test environment.
Any ideas would be great.
Upvotes: 0
Views: 877
Reputation: 241673
find_by_zipcode
is not part of the main ActiveSupport::TimeZone
object. The docs for that object are here, and you won't find any mention of zip codes.
A Google search found that method as part of the TZip gem. Since you said it works in your application, I would guess that you have that gem there. You probably just need to add it to your test project. (Sorry, not familiar with Ruby or RSpec all that well, so can't guide you there).
Being quite familiar with time zones, I thought I would also take this opportunity to address a few concerns about the general idea of mapping zip codes to time zones. I'm not so sure that it is a great idea.
So my recommendation would be to avoid this approach entirely. Instead, use one of the methods described in this community wiki.
Upvotes: 1