如何“解析”?在测试环境中未定义 Time 类,但在开发或生产环境中未定义 Time 类?
我在运行测试(Steak + Capybara)时遇到一个奇怪的问题,该测试请求某个页面,但出现错误: undefined method parse for Time:Class
该方法调用发生在名为 type.rb
的 Sunspot 文件中:
def value_to_utc_time(value)
if value.respond_to?(:utc)
value.utc
elsif value.respond_to?(:new_offset)
value.new_offset
else
begin
Time.parse(value.to_s).utc
rescue ArgumentError
DateTime.parse(value.to_s).new_offset
end
end
end
当我在 RAILS_ENV=test
中运行服务器时,我得到同样的错误。当我在开发中运行服务器时,一切都很好。这似乎表明不仅仅是我的测试框架,而是整个测试环境都导致了这个错误。
到目前为止,我在网上能找到的关于 Time.parse
未定义的唯一信息非常没有帮助,大多数人说添加 require 'time'
将解决问题。
我的问题有两个:
- 这很糟糕,
- 我已经在不同的地方尝试过它(在 config/environments/test.rb 中,在测试文件本身等),但它只是没有工作。
如果有人对发生的事情有任何想法,我欢迎他们!
I'm encountering a strange issue while running a test (Steak + Capybara) that requests a certain page where I get the error:undefined method parse for Time:Class
The method call occurs in a Sunspot file called type.rb
:
def value_to_utc_time(value)
if value.respond_to?(:utc)
value.utc
elsif value.respond_to?(:new_offset)
value.new_offset
else
begin
Time.parse(value.to_s).utc
rescue ArgumentError
DateTime.parse(value.to_s).new_offset
end
end
end
When I run my server in RAILS_ENV=test
, I get the same error. When I run the server in development, everything is fine and dandy. This would seem to indicate that it's not just my test framework, but the whole test environment that's promoting this error.
The only info I could find online so far about Time.parse
being undefined has been quite unhelpful, with most people saying that adding require 'time'
will solve the problem.
My problems with this are twofold:
- It's hacky
- I've tried it in various places (in
config/environments/test.rb
, in the test file itself, etc.), and it just doesn't work.
If anyone has any ideas what is going on, I welcome them!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了!有人在 Rails 应用程序的测试目录中创建了另一个名为
time.rb
的文件,因此测试环境中对 Time 类的方法调用调用的是该文件,而不是实际的 Time 类。我从这个 Github 问题线程中得到了检查这种可能性的灵感I figured it out! Someone had created another file called
time.rb
in the Rails app's test directory, so method calls on the Time class in the test environment were invoking that file rather than the actual Time class. I got the inspiration to check for this possibility from this Github issue thread