diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-14 00:50:42 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-14 00:50:42 +0000 |
commit | 3ab1cb6a6a1ea0bde8ef35d02370248396753c7a (patch) | |
tree | 74b4a09495012ee1e50cd74dab05f9300b1c9499 | |
parent | d54406271b0b38614035e08c611fbae048b60ec3 (diff) | |
download | rails-3ab1cb6a6a1ea0bde8ef35d02370248396753c7a.tar.gz rails-3ab1cb6a6a1ea0bde8ef35d02370248396753c7a.tar.bz2 rails-3ab1cb6a6a1ea0bde8ef35d02370248396753c7a.zip |
Mock Time.now for more accurate Touch mixin tests. Closes #6213.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5106 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/test/mixin_test.rb | 54 |
2 files changed, 46 insertions, 10 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 97573e272f..a30105605d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Mock Time.now for more accurate Touch mixin tests. #6213 [Dan Peterson] + * Improve yaml fixtures error reporting. #6205 [Bruce Williams] * Rename AR::Base#quote so people can use that name in their models. #3628 [Koz] diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index 8ab73cfe7f..9318c3a1e1 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -4,6 +4,23 @@ require 'active_record/acts/list' require 'active_record/acts/nested_set' require 'fixtures/mixin' +# Let us control what Time.now returns for the TouchTest suite +class Time + @@forced_now_time = nil + cattr_accessor :forced_now_time + + class << self + def now_with_forcing + if @@forced_now_time + @@forced_now_time + else + now_without_forcing + end + end + alias_method_chain :now, :forcing + end +end + class ListTest < Test::Unit::TestCase fixtures :mixins @@ -304,6 +321,23 @@ end class TouchTest < Test::Unit::TestCase fixtures :mixins + + def setup + Time.forced_now_time = Time.now + end + + def teardown + Time.forced_now_time = nil + end + + def test_time_mocking + five_minutes_ago = 5.minutes.ago + Time.forced_now_time = five_minutes_ago + assert_equal five_minutes_ago, Time.now + + Time.forced_now_time = nil + assert_not_equal five_minutes_ago, Time.now + end def test_update stamped = Mixin.new @@ -311,14 +345,14 @@ class TouchTest < Test::Unit::TestCase assert_nil stamped.updated_at assert_nil stamped.created_at stamped.save - assert_not_nil stamped.updated_at - assert_not_nil stamped.created_at + assert_equal Time.now, stamped.updated_at + assert_equal Time.now, stamped.created_at end def test_create - @obj = Mixin.create - assert_not_nil @obj.updated_at - assert_not_nil @obj.created_at + obj = Mixin.create + assert_equal Time.now, obj.updated_at + assert_equal Time.now, obj.created_at end def test_many_updates @@ -327,16 +361,16 @@ class TouchTest < Test::Unit::TestCase assert_nil stamped.updated_at assert_nil stamped.created_at stamped.save - assert_not_nil stamped.created_at - assert_not_nil stamped.updated_at + assert_equal Time.now, stamped.created_at + assert_equal Time.now, stamped.updated_at old_updated_at = stamped.updated_at - sleep 1 + Time.forced_now_time = 5.minutes.from_now stamped.save - assert_not_equal stamped.created_at, stamped.updated_at - assert_not_equal old_updated_at, stamped.updated_at + assert_equal Time.now, stamped.updated_at + assert_equal old_updated_at, stamped.created_at end def test_create_turned_off |