问题描述
我正在使用 ActiveSupport::TimeZone 根据邮政编码在某个位置设置时区.
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
这在应用程序本身中工作得很好.我在 before_save 上调用 set_time_zone.
This works just fine in the application itself. I am calling the set_time_zone on before_save.
在使用 Rspec 运行测试时,当它尝试运行 set_time_zone 方法时,它会出错,并显示ActiveSupport::TimeZone 中的未定义方法‘find_by_zipcode’"
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"
我的规范助手中也包含了require 'active_support/time_with_zone'".
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.
任何想法都会很棒.
推荐答案
find_by_zipcode
不是主要的 ActiveSupport::TimeZone
对象的一部分.该对象的文档在此处,您将找不到任何提及邮政编码.
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.
Google 搜索发现该方法是 TZip gem 的一部分.既然你说它适用于你的应用程序,我猜你那里有那个宝石.您可能只需要将它添加到您的测试项目中.(抱歉,对 Ruby 或 RSpec 不太熟悉,所以无法指导您).
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.
- 它非常专注于美国.时区是全球性的,邮政编码仅在美国有效.
- 邮政编码经常变化.USPS 发布了您可以订阅以更改此数据的数据库.它会出现在 TZip 提交历史 和 问题跟踪器 他们一直在报告问题时手动添加邮政编码映射.这不是处理频繁变化的数据的好方法.
- 邮政编码不是识别位置的最佳边界.有许多邮政编码涵盖不同的、不连续的区域.还有一些行政邮政编码不会映射到任何特定位置(例如海外军事邮件的邮政编码).
- 对于为特定邮政编码分配纬度和经度的数据库,这些坐标通常是人为选择的,作为该邮政编码所服务区域的质心的近似值.同样,这不是一个离散的位置.
- 根据TZip 源代码,这些映射仅涵盖 7 个时区.他们忘记了也有邮政编码的美国领土,例如关岛.其他国家(如波多黎各)被错误地映射到东部时区而不是大西洋时区.
- It is very U.S. focused. Time Zones are worldwide, and zip codes only work in the USA.
- Zip codes change frequently. The USPS publishes databases that you can subscribe to for changes to this data. It would appear from the TZip commit history and issue tracker that they have been manually adding zip code mappings as problems are reported. This is not a good way to handle data that is frequently changing.
- A zip code is not the best boundary to identify a location. There are many zip codes that cover disparate, non-contiguous areas. There are also administrative zip codes that don't map to any particular location (like those for overseas military mail).
- For those databases that assign a latitude and longitude to a particular zip code, those coordinates are often artificially chosen, as an approximation of the centroid of the area serviced by that zip code. Again, this is not a discrete location.
- According to the TZip source code, there are only 7 time zones covered by these mappings. They have forgotten about US territories that also have zip codes, such as Guam. Others, like Puerto Rico have been erroneously mapped to the Eastern time zone instead of the Atlantic time zone.
所以我的建议是完全避免这种方法.相反,请使用在此社区维基中描述的方法之一一>.
So my recommendation would be to avoid this approach entirely. Instead, use one of the methods described in this community wiki.
这篇关于ActiveSupport::TimeZone 在 Rspec 测试中无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!