Trouble comparing time with RSpec

后端 未结 7 750
北荒
北荒 2020-12-12 15:23

I am using Ruby on Rails 4 and the rspec-rails gem 2.14. For a my object I would like to compare the current time with the updated_at object attribute after a c

7条回答
  •  孤城傲影
    2020-12-12 15:45

    Old post, but I hope it helps anyone who enters here for a solution. I think it's easier and more reliable to just create the date manually:

    it "updates updated_at attribute" do
      freezed_time = Time.utc(2015, 1, 1, 12, 0, 0) #Put here any time you want
      Timecop.freeze(freezed_time) do
        patch :update
        @article.reload
        expect(@article.updated_at).to eq(freezed_time)
      end
    end
    

    This ensures the stored date is the right one, without doing to_x or worrying about decimals.

提交回复
热议问题