time_helpers.rb 3.4 KB
Newer Older
1 2
module ActiveSupport
  module Testing
3
    class SimpleStubs # :nodoc:
4 5 6 7 8 9 10 11 12 13 14 15 16
      Stub = Struct.new(:object, :method_name, :original_method)

      def initialize
        @stubs = {}
      end

      def stub_object(object, method_name, return_value)
        key = [object.object_id, method_name]

        if (stub = @stubs[key])
          unstub_object(stub)
        end

17
        new_name = "__simple_stub__#{method_name}"
18

19 20 21
        @stubs[key] = Stub.new(object, method_name, new_name)

        object.singleton_class.send :alias_method, new_name, method_name
22 23 24 25
        object.define_singleton_method(method_name) { return_value }
      end

      def unstub_all!
R
Rafael Mendonça França 已提交
26
        @stubs.each_value do |stub|
27 28 29 30 31
          unstub_object(stub)
        end
        @stubs = {}
      end

32 33 34
      private

        def unstub_object(stub)
35 36 37 38
          singleton_class = stub.object.singleton_class
          singleton_class.send :undef_method, stub.method_name
          singleton_class.send :alias_method, stub.method_name, stub.original_method
          singleton_class.send :undef_method, stub.original_method
39
        end
40 41
    end

42 43
    # Containing helpers that helps you test passage of time.
    module TimeHelpers
44
      def after_teardown #:nodoc:
45
        simple_stubs.unstub_all!
46 47 48
        super
      end

49
      # Change current time to the time in the future or in the past by a given time difference by
50 51
      # stubbing +Time.now+ and +Date.today+. Note that the stubs are automatically removed
      # at the end of each test.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      #
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      #   travel 1.day
      #   Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
      #   Date.current # => Sun, 10 Nov 2013
      #
      # This method also accepts a block, which will return the current time back to its original
      # state at the end of the block:
      #
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      #   travel 1.day do
      #     User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
      #   end
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      def travel(duration, &block)
        travel_to Time.now + duration, &block
      end

      # Change current time to the given time by stubbing +Time.now+ and +Date.today+ to return the
71 72
      # time or date passed into this method. Note that the stubs are automatically removed
      # at the end of each test.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      #
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      #   travel_to Time.new(2004, 11, 24, 01, 04, 44)
      #   Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
      #   Date.current # => Wed, 24 Nov 2004
      #
      # This method also accepts a block, which will return the current time back to its original
      # state at the end of the block:
      #
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      #   travel_to Time.new(2004, 11, 24, 01, 04, 44) do
      #     User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
      #   end
      #   Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
      def travel_to(date_or_time, &block)
88 89
        simple_stubs.stub_object(Time, :now, date_or_time.to_time)
        simple_stubs.stub_object(Date, :today, date_or_time.to_date)
90 91 92

        if block_given?
          block.call
93
          simple_stubs.unstub_all!
94 95
        end
      end
96

97 98 99 100 101
      private

        def simple_stubs
          @simple_stubs ||= SimpleStubs.new
        end
102 103 104
    end
  end
end