Sha256: ca6b2db0061ae0e0fb60459b562c592c501bd2b589229bce3691a1b7cad9203a

Contents?: true

Size: 1.99 KB

Versions: 105

Compression:

Stored size: 1.99 KB

Contents

require "cases/helper"

class Mixin < ActiveRecord::Base
end

# 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 TouchTest < ActiveRecord::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

    assert_nil stamped.updated_at
    assert_nil stamped.created_at
    stamped.save
    assert_equal Time.now, stamped.updated_at
    assert_equal Time.now, stamped.created_at
  end

  def test_create
    obj = Mixin.create
    assert_equal Time.now, obj.updated_at
    assert_equal Time.now, obj.created_at
  end

  def test_many_updates
    stamped = Mixin.new

    assert_nil stamped.updated_at
    assert_nil stamped.created_at
    stamped.save
    assert_equal Time.now, stamped.created_at
    assert_equal Time.now, stamped.updated_at

    old_updated_at = stamped.updated_at

    Time.forced_now_time = 5.minutes.from_now
    stamped.lft_will_change!
    stamped.save

    assert_equal Time.now, stamped.updated_at
    assert_equal old_updated_at, stamped.created_at
  end

  def test_create_turned_off
    Mixin.record_timestamps = false

    mixin = Mixin.new

    assert_nil mixin.updated_at
    mixin.save
    assert_nil mixin.updated_at

  # Make sure Mixin.record_timestamps gets reset, even if this test fails,
  # so that other tests do not fail because Mixin.record_timestamps == false
  rescue Exception => e
    raise e
  ensure
    Mixin.record_timestamps = true
  end

end

Version data entries

105 entries across 102 versions & 21 rubygems

Version Path
webroar-0.4.0 src/admin_panel/vendor/rails/activerecord/test/cases/mixin_test.rb
nbudin-castronaut-0.7.5 vendor/activerecord/test/cases/mixin_test.rb
radiant-0.9.1 vendor/rails/activerecord/test/cases/mixin_test.rb
activerecord-2.3.8 test/cases/mixin_test.rb
activerecord-2.3.8.pre1 test/cases/mixin_test.rb
activerecord-2.3.7 test/cases/mixin_test.rb
activerecord-2.3.6 test/cases/mixin_test.rb
radiant-0.8.2 vendor/rails/activerecord/test/cases/mixin_test.rb
radiant-0.9.0.rc2 vendor/rails/activerecord/test/cases/mixin_test.rb
webroar-0.3.1 src/admin_panel/vendor/rails/activerecord/test/cases/mixin_test.rb
webroar-0.3.0 src/admin_panel/vendor/rails/activerecord/test/cases/mixin_test.rb
usher-0.7.0 spec/rails2_3/vendor/rails/vendor/rails/activerecord/pkg/activerecord-2.3.3/test/cases/mixin_test.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activerecord/pkg/activerecord-2.2.2/test/cases/mixin_test.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activerecord/test/cases/mixin_test.rb
usher-0.7.0 spec/rails2_3/vendor/rails/vendor/rails/activerecord/test/cases/mixin_test.rb
simply_stated-0.0.5 vendor/gems/ruby/1.8/gems/activerecord-2.3.5/test/cases/mixin_test.rb
simply_stated-0.0.4 vendor/gems/ruby/1.8/gems/activerecord-2.3.5/test/cases/mixin_test.rb
factorylabs-castronaut-0.7.5 vendor/activerecord/test/cases/mixin_test.rb
ginst-2009.12.8 vendor/rails/activerecord/test/cases/mixin_test.rb
activerecord-2.3.5 test/cases/mixin_test.rb