added time_travel plugin for validation specs to fix time from Time.now

This commit is contained in:
Adam Meehan 2008-07-07 15:12:28 +10:00
parent bf6aeea841
commit 494f3bdef4
3 changed files with 44 additions and 20 deletions

View File

@ -2,26 +2,31 @@ $: << File.dirname(__FILE__) + '/../lib' << File.dirname(__FILE__)
require 'rubygems'
require 'spec'
require 'spec/interop/test'
if File.exists?(File.dirname(__FILE__) + '/../../../../vendor/rails')
$:.unshift File.dirname(__FILE__) + '/../../../../vendor/rails'
require 'activesupport/lib/active_support'
require 'activerecord/lib/active_record'
require 'activerecord/lib/active_record/version'
require 'railties/lib/rails/version'
vendored = true
else
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
if vendored = File.exists?(vendored_rails)
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
else
gem 'rails', "=#{ENV['VERSION']}" if ENV['VERSION']
require 'rails/version'
require 'active_record'
require 'active_record/version'
vendored = false
end
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{Rails::VERSION::STRING} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
require 'rails/version'
require 'active_record'
require 'active_record/version'
require 'action_controller'
require 'action_controller/test_process'
require 'action_view'
require 'rspec-rails'
require 'time_travel/time_travel'
require 'validates_timeliness'
RAILS_VER = Rails::VERSION::STRING
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
ActiveRecord::Base.default_timezone = :utc
if RAILS_VER >= '2.1'
@ -29,8 +34,6 @@ if RAILS_VER >= '2.1'
ActiveRecord::Base.time_zone_aware_attributes = true
end
require 'validates_timeliness'
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
require 'resources/schema'

1
spec/time_travel Submodule

@ -0,0 +1 @@
Subproject commit 7d4e697b1cee90bb1f34ac302881ce0641e654ec

View File

@ -1,6 +1,14 @@
require File.dirname(__FILE__) + '/spec_helper'
describe ValidatesTimeliness::Validations do
before :all do
Time.now = Time.utc(2008, 1, 1, 12, 0, 0)
end
after :all do
Time.now = nil
end
describe "with no restrictions" do
before :all do
class BasicValidation < Person
@ -56,7 +64,7 @@ describe ValidatesTimeliness::Validations do
before :all do
class TimeBeforeAfter < Person
validates_timeliness_of :birth_date_and_time, :before => Time.now, :after => 1.day.ago
end
end
end
before :each do
@ -74,6 +82,18 @@ describe ValidatesTimeliness::Validations do
@person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be after/)
end
it "should have error when on boundary of :before restriction" do
@person.birth_date_and_time = Time.now
@person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be before/)
end
it "should have error when on boundary of :after restriction" do
@person.birth_date_and_time = 1.day.ago
@person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be after/)
end
end
describe "with on_or_before and on_or_after restrictions" do
@ -83,18 +103,18 @@ describe ValidatesTimeliness::Validations do
end
end
before :each do
before do
@person = TimeOnOrBeforeAndAfter.new
end
it "should have error when past :on_or_before restriction" do
@person.birth_date_and_time = 1.minute.from_now
@person.birth_date_and_time = Time.now.at_midnight + 1
@person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be on or before/)
end
it "should have error when before :on_or_after restriction" do
@person.birth_date_and_time = 2.days.ago
@person.birth_date_and_time = 1.days.ago - 1
@person.should_not be_valid
@person.errors.on(:birth_date_and_time).should match(/must be on or after/)
end
@ -107,7 +127,7 @@ describe ValidatesTimeliness::Validations do
it "should be valid when value equal to :on_or_after restriction" do
@person.birth_date_and_time = 1.day.ago
@person.should be_valid
end
end
end
end