【问题标题】:How to get TimeWithZone within rails3 unit/test如何在 rails3 单元/测试中获取 TimeWithZone
【发布时间】:2010-10-16 18:03:25
【问题描述】:

我正在尝试测试一个轨道、应用程序,在一种特殊情况下,我需要在我的测试用例中创建一个 TimeWithZone 对象。所以我这样写:

started_at = ActiveSupport::TimeWithZone(started_at, Time.zone)

只是为了得到一个错误:

NoMethodError: undefined method `TimeWithZone' for ActiveSupport:Module

我尝试要求 'active_support'、'active_support/time'、''active_support/time_with_zone'。当我运行测试时,这些语句中的每一个都评估为 false(在 irb 中运行良好)

知道我做错了什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby activesupport


    【解决方案1】:

    尝试创建一个new 对象:

    started_at = ActiveSupport::TimeWithZone.new(started_at, Time.zone)
    

    但是,您永远不需要直接创建此类的实例。

    如果您已经正确设置了 Time.zone,您可以简单地使用 at 方法:

    started_at_with_zone = Time.zone.at(started_at)
    

    查看Rails API documentation了解详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2012-03-28
      • 2011-11-19
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多