mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:28:11 +08:00
Try to fix specs that are failing in jenkins
This commit is contained in:
parent
1355c1e3b0
commit
8e50b49cf1
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
8
spec/support/time_matcher.rb
Normal file
8
spec/support/time_matcher.rb
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user