ruby-on-rails – Rails – 测试使用DateTime.now的方法

我有一个使用DateTime.Now对某些数据执行搜索方法,我想测试各种日期的方法,但是我不知道如何存储DateTime.Now,也不能让它与Timecop一起工作(如果它甚至这样做).

有时间警察我试过

it 'has the correct amount if falls in the prevIoUs month' do
      t = "25 May".to_datetime
      Timecop.travel(t)
      puts DateTime.Now

      expect(@employee.monthly_sales).to eq 150
end

当我运行规范我可以看到放置DateTime.Now给2015-05-25T01:00:00 01:00但有相同的放置DateTime.Now在我测试输出方法2015-07-24T08:57: 53 01:00(今天).
我该如何做到这一点?

——————更新——————————- ——————–

我正在将一个before(所有)块中的记录(@employee等)设置成似乎已经导致了这个问题.它只有在Timecop做阻止之后完成设置时才有效.为什么会这样?

解决方法

TL; DR:问题是在Timecop.freeze在规范中调用之后,在Employee中调用了DateTime.Now.

Timecop模拟Time,Date和DateTime的构造函数.在冻结和返回之间创建的任何实例(或冻结块内)都将被嘲笑.
在冻结或返回后创建的任何实例都不会受到影响,因为Timecop不会混淆现有对象.

README(我的重点):

A gem providing “time travel” and “time freezing” capabilities,making it dead simple to test time-dependent code. It provides a unified method to mock Time.Now,Date.today,and DateTime.Now in a single call.

所以在创建要嘲笑的Time对象之前调用Timecop.freeze是至关重要的.如果您在阻止之前冻结了RSpec,则会在对象进行评估之前运行.但是,如果您有一个在您设置主题的前一个块(在您的案例中为@employee),并且在嵌套描述中还有另一个块,那么您的主题已经设置,在冻结时间之前调用了DateTime.new.

如果将以下内容添加到员工中,会发生什么

class Employee
  def Now
    DateTime.Now
  end
end

然后运行以下规范:

describe '#Now' do
  let(:employee) { @employee }
  it 'has the correct amount if falls in the prevIoUs month',focus: true do
    t = "25 May".to_datetime
    Timecop.freeze(t) do
      expect(DateTime.Now).to eq t
      expect(employee.Now).to eq t

      expect(employee.Now.class).to be DateTime
      expect(employee.Now.class.object_id).to be DateTime.object_id
    end
  end
end

而不是使用冻结块,您还可以在钩子之前和之后冻结并返回rspec:

describe Employee do
  let(:frozen_time) { "25 May".to_datetime }
  before { Timecop.freeze(frozen_time) }
  after { Timecop.return }
  subject { FactoryGirl.create :employee }

  it 'has the correct amount if falls in the prevIoUs month' do
    # spec here
  end

end

偏离主题,可能看看http://betterspecs.org/

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...