From 8e50b49cf1c48c54c861c05a0adb701f6bc7fb27 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 27 Jun 2013 17:23:06 -0400 Subject: [PATCH] Try to fix specs that are failing in jenkins --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 54 ++++++++++++++++++++++++++++-------- spec/support/time_matcher.rb | 8 ++++++ 3 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 spec/support/time_matcher.rb diff --git a/app/models/user.rb b/app/models/user.rb index 7021cc87468..c911ff0fe00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -283,7 +283,7 @@ class User < ActiveRecord::Base end def update_last_seen! - now = DateTime.now + now = Time.zone.now now_date = now.to_date # Only update last seen once every minute redis_key = "user:#{self.id}:#{now_date.to_s}" diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d7c4a0cbdb1..58806d80709 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -584,6 +584,7 @@ describe User do end describe "previous_visit_at" do + let(:user) { Fabricate(:user) } before do @@ -595,13 +596,17 @@ describe User do end describe "first time" do - let!(:first_visit_date) { DateTime.now } + let!(:first_visit_date) { Time.zone.now } before do - DateTime.stubs(:now).returns(first_visit_date) + Timecop.freeze(first_visit_date) user.update_last_seen! end + after do + Timecop.return + end + it "should have no value" do user.previous_visit_at.should be_nil end @@ -609,10 +614,14 @@ describe User do describe "another call right after" do before do # A different time, to make sure it doesn't change - DateTime.stubs(:now).returns(10.minutes.from_now) + Timecop.freeze(10.minutes.from_now) user.update_last_seen! end + after do + Timecop.return + end + it "still has no value" do user.previous_visit_at.should be_nil end @@ -622,24 +631,33 @@ describe User do let!(:second_visit_date) { 2.hours.from_now } before do - DateTime.stubs(:now).returns(second_visit_date) + Timecop.freeze(second_visit_date) user.update_last_seen! end + after do + Timecop.return + end + it "should have the previous visit value" do - user.previous_visit_at.should == first_visit_date + user.reload + user.previous_visit_at.should be_within_one_second_of(first_visit_date) end describe "third visit" do let!(:third_visit_date) { 5.hours.from_now } before do - DateTime.stubs(:now).returns(third_visit_date) + Timecop.freeze(third_visit_date) user.update_last_seen! end + after do + Timecop.return + end + it "should have the second visit value" do - user.previous_visit_at.should == second_visit_date + user.previous_visit_at.should be_within_one_second_of(second_visit_date) end end @@ -662,15 +680,19 @@ describe User do end describe 'with no previous values' do - let!(:date) { DateTime.now } + let!(:date) { Time.zone.now } before do - DateTime.stubs(:now).returns(date) + Timecop.freeze(date) user.update_last_seen! end + after do + Timecop.return + end + it "updates last_seen_at" do - user.last_seen_at.should == date + user.last_seen_at.should be_within_one_second_of(date) end it "should have 0 for days_visited" do @@ -685,12 +707,16 @@ describe User do context "called twice" do before do - DateTime.stubs(:now).returns(date) + Timecop.freeze(date) user.update_last_seen! user.update_last_seen! user.reload end + after do + Timecop.return + end + it "doesn't increase days_visited twice" do user.days_visited.should == 1 end @@ -701,10 +727,14 @@ describe User do let!(:future_date) { 3.days.from_now } before do - DateTime.stubs(:now).returns(future_date) + Timecop.freeze(future_date) user.update_last_seen! end + after do + Timecop.return + end + it "should log a second visited_at record when we log an update later" do user.user_visits.count.should == 2 end diff --git a/spec/support/time_matcher.rb b/spec/support/time_matcher.rb new file mode 100644 index 00000000000..f50f9c1f88b --- /dev/null +++ b/spec/support/time_matcher.rb @@ -0,0 +1,8 @@ +RSpec::Matchers.define :be_within_one_second_of do |expected_time| + match do |actual_time| + (actual_time - expected_time).abs < 1 + end + failure_message_for_should do |actual_time| + "#{actual_time.to_s} is not within 1 second of #{expected_time}" + end +end \ No newline at end of file