使用Timecop的意外Rspec结果

时间:2012-06-28 20:32:19

标签: ruby-on-rails rspec timecop

以下是规范的基本概念:

before :each do
  Timecop.freeze(Time.local(2012, 07, 01, 12, 0, 0)) #frozen at July 1st, 2012 at noon
  #create record code
end
it 'shows how long ago the message was recieved' do
  Timecop.travel(Time.local(2012, 07, 02, 12, 0, 0)) #move to July 2nd
  page.should have_content "1 day ago"
end
after :each do
  Timecop.return #release freeze
end

它出错:

expected there to be content "1 day ago" in "less than a minute ago"

我正在显示<%= "#{time_ago_in_words(m.created_at)} ago" %>,我希望它与24小时不同。我错过了什么?

1 个答案:

答案 0 :(得分:0)

问题在于误解了旅行方法与冻结方法的目的。

冻结在所选时刻停止时间,旅行将时间设定为所选时刻,但它会从那里自由行进。

我通过在示例中替换Travel with Freeze来解决问题。