Merge pull request #347 from ChrisBr/fix_test_dates

Implements timecop to make tests independent from DateTime
This commit is contained in:
Christian Bruckmayer 2014-07-23 15:20:44 +02:00
commit 35c5116051
14 changed files with 42 additions and 38 deletions

View file

@ -127,6 +127,7 @@ group :test do
gem 'shoulda' gem 'shoulda'
# Extracted from RSpec 3 stub_model and mock_model # Extracted from RSpec 3 stub_model and mock_model
gem 'rspec-activemodel-mocks' gem 'rspec-activemodel-mocks'
gem 'timecop'
end end
group :development, :test do group :development, :test do

View file

@ -362,6 +362,7 @@ GEM
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.4) thread_safe (0.3.4)
tilt (1.4.1) tilt (1.4.1)
timecop (0.7.1)
timers (1.1.0) timers (1.1.0)
tins (1.1.0) tins (1.1.0)
transitions (0.1.12) transitions (0.1.12)
@ -443,6 +444,7 @@ DEPENDENCIES
shoulda shoulda
spring-commands-rspec spring-commands-rspec
sqlite3 sqlite3
timecop
transitions transitions
turbolinks turbolinks
uglifier (>= 1.3.0) uglifier (>= 1.3.0)

View file

@ -43,6 +43,9 @@ Osem::Application.configure do
config.after_initialize do config.after_initialize do
ActiveRecord::Base.logger = nil ActiveRecord::Base.logger = nil
# Set Time.now to May 1, 2014 00:01:00 AM (at this instant), but allow it to move forward
t = Time.local(2014, 05, 01, 00, 01, 00)
Timecop.travel(t)
end end
end end

View file

@ -2,8 +2,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :call_for_papers do factory :call_for_papers do
start_date Date.today - 1 start_date { 1.day.ago }
end_date Date.today + 7 end_date { 7.days.from_now }
description 'We call for papers' description 'We call for papers'
conference conference
end end

View file

@ -7,10 +7,10 @@ FactoryGirl.define do
social_tag 'dps14' social_tag 'dps14'
timezone 'Amsterdam' timezone 'Amsterdam'
contact_email 'admin@example.com' contact_email 'admin@example.com'
start_date Date.today start_date { Date.today }
end_date Date.today + 6.days end_date { 6.days.from_now }
registration_start_date Date.today + 3.days registration_start_date { 3.days.from_now }
registration_end_date Date.today + 5.days registration_end_date { 5.days.from_now }
make_conference_public true make_conference_public true
venue venue
end end

View file

@ -5,7 +5,7 @@ FactoryGirl.define do
picture_file_name 'rails.png' picture_file_name 'rails.png'
picture_content_type 'image/png' picture_content_type 'image/png'
picture_file_size '1024' picture_file_size '1024'
picture_updated_at DateTime.now picture_updated_at { DateTime.now }
description 'Lorem Ipsum Dolor' description 'Lorem Ipsum Dolor'
conference conference
end end

View file

@ -2,7 +2,7 @@ FactoryGirl.define do
factory :social_event do factory :social_event do
title 'Example Social Event' title 'Example Social Event'
description 'Lorem Ipsum Dolsum' description 'Lorem Ipsum Dolsum'
date Date.today date { Date.today }
conference conference
end end

View file

@ -2,7 +2,7 @@
FactoryGirl.define do FactoryGirl.define do
factory :target do factory :target do
due_date Date.today + 14 due_date { 14.days.from_now }
target_count 100 target_count 100
unit Target.units[:submissions] unit Target.units[:submissions]
end end

View file

@ -5,7 +5,7 @@ FactoryGirl.define do
sequence(:name) { |n| "name#{n}" } sequence(:name) { |n| "name#{n}" }
password 'changeme' password 'changeme'
password_confirmation 'changeme' password_confirmation 'changeme'
confirmed_at Time.now confirmed_at { Time.now }
biography <<-EOS biography <<-EOS
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim
nunc, venenatis non sapien convallis, dictum suscipit purus. Vestibulum nunc, venenatis non sapien convallis, dictum suscipit purus. Vestibulum

View file

@ -1,6 +1,6 @@
FactoryGirl.define do FactoryGirl.define do
factory :vday do factory :vday do
day Date.today day { Date.today }
description 'Lorem Ipsum dolsum' description 'Lorem Ipsum dolsum'
conference conference
end end

View file

@ -18,17 +18,17 @@ feature Conference do
click_link 'Add vday' click_link 'Add vday'
expect(page.all('div.nested-fields').count == 1).to be true expect(page.all('div.nested-fields').count == 1).to be true
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(1)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(1)').
set("#{Date.today.strftime('%Y')}") select("#{Date.today.strftime('%Y')}")
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(2)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(2)').
set("#{Date.today.strftime('%B')}") select("#{Date.today.strftime('%B')}")
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(3)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(3)').
set("#{Date.today.strftime('%-d')}") select("#{Date.today.strftime('%-d')}")
page. page.
find('div.nested-fields:nth-of-type(1) div:nth-of-type(1) textarea'). find('div.nested-fields:nth-of-type(1) div:nth-of-type(1) textarea').
set('Example Person') set('Example Person')
click_button 'Update Conference' click_button 'Update Conference'
expect(flash). expect(flash).
to eq('Volunteering options were successfully updated.') to eq('Volunteering options were successfully updated.')
@ -64,17 +64,17 @@ feature Conference do
click_link 'Add vday' click_link 'Add vday'
expect(page.all('div.nested-fields').count == 1).to be true expect(page.all('div.nested-fields').count == 1).to be true
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(1)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(1)').
set("#{Date.today.strftime('%Y')}") select("#{Date.today.strftime('%Y')}")
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(2)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(2)').
set("#{Date.today.strftime('%B')}") select("#{Date.today.strftime('%B')}")
page. page.
find('div.nested-fields:nth-of-type(1) select:nth-of-type(3)'). find('div.nested-fields:nth-of-type(1) select:nth-of-type(3)').
set("#{Date.today.strftime('%-d')}") select("#{Date.today.strftime('%-d')}")
page. page.
find('div.nested-fields:nth-of-type(1) div:nth-of-type(1) textarea'). find('div.nested-fields:nth-of-type(1) div:nth-of-type(1) textarea').
set('Example Person') set('Example Person')
click_button 'Update Conference' click_button 'Update Conference'
expect(flash). expect(flash).
to eq('Volunteering options were successfully updated.') to eq('Volunteering options were successfully updated.')

View file

@ -717,6 +717,7 @@ describe Conference do
it 'returns the last two past conferences if there are no pending conferences' do it 'returns the last two past conferences if there are no pending conferences' do
subject.start_date = Time.now - 10.days subject.start_date = Time.now - 10.days
subject.end_date = Time.now - 5.days subject.end_date = Time.now - 5.days
subject.save
c = create(:conference, c = create(:conference,
short_title: 'c', start_date: Time.now - 1.year, short_title: 'c', start_date: Time.now - 1.year,
end_date: Time.now - 360.days) end_date: Time.now - 360.days)
@ -901,9 +902,9 @@ describe Conference do
let!(:admin_role) { create(:admin_role) } let!(:admin_role) { create(:admin_role) }
it 'self#event_distribution calculates correct values with user' do it 'self#event_distribution calculates correct values with user' do
create(:user, last_sign_in_at: Time.now - 3.months) # active create(:user, last_sign_in_at: Date.today - 3.months) # active
create(:user, confirmed_at: nil) # unconfirmed create(:user, confirmed_at: nil) # unconfirmed
create(:user, last_sign_in_at: Time.now - 1.year - 1.day) # dead create(:user, last_sign_in_at: Date.today - 1.year - 1.day) # dead
result = {} result = {}
result['Active'] = { 'color' => 'green', 'value' => 1 } result['Active'] = { 'color' => 'green', 'value' => 1 }
result['Unconfirmed'] = { 'color' => 'red', 'value' => 1 } result['Unconfirmed'] = { 'color' => 'red', 'value' => 1 }
@ -913,7 +914,7 @@ describe Conference do
end end
it 'self#event_distribution calculates correct with only active user' do it 'self#event_distribution calculates correct with only active user' do
create(:user, last_sign_in_at: Time.now - 3.months) # active create(:user, last_sign_in_at: Date.today - 3.months) # active
result = {} result = {}
result['Active'] = { 'color' => 'green', 'value' => 1 } result['Active'] = { 'color' => 'green', 'value' => 1 }

View file

@ -97,7 +97,7 @@ describe Target do
target = build(:target, target_count: 10, unit: Target.units[:registrations]) target = build(:target, target_count: 10, unit: Target.units[:registrations])
conference.targets = [target] conference.targets = [target]
result = "10 Registrations by #{Date.today + 14}" result = "10 Registrations by #{14.days.from_now.to_date}"
expect(target.to_s).to eq(result) expect(target.to_s).to eq(result)
end end

View file

@ -3,15 +3,12 @@ require 'spec_helper'
describe 'admin/callforpapers/show' do describe 'admin/callforpapers/show' do
it 'renders callforpapers details' do it 'renders callforpapers details' do
@conference = create(:conference) assign :conference, create(:conference)
assign :conference, @conference assign :cfp, create(:call_for_papers)
assign :cfp, stub_model(CallForPapers, start_date: Date.today,
end_date: Date.today + 7.days,
description: 'Lorem Ipsum Dolsum')
render render
expect(rendered).to include(Date.today.strftime('%Y-%m-%d')) expect(rendered).to include(1.day.ago.strftime('%Y-%m-%d'))
expect(rendered).to include(7.days.from_now.strftime('%Y-%m-%d')) expect(rendered).to include(7.days.from_now.strftime('%Y-%m-%d'))
expect(rendered).to include('Lorem Ipsum Dolsum') expect(rendered).to include('We call for papers')
end end
end end