Bugs: Browse | Submit New | Admin

[#19801] Time mock broken

Date:
2008-04-27 08:38
Priority:
3
Submitted By:
Nobody
Assigned To:
Nobody (None)
Category:
None
State:
Open
Summary:
Time mock broken

Detailed description
Setup a new project, Ruby 1.8.6, Rails 2.0.2, followed the instructions in README_USER_LOGIN and attempted to run the
functional tests

K:\development\ruby\rails\contact>rake test:functionals
(in K:/development/ruby/rails/contact)
c:/ruby/bin/ruby -Ilib;test "c:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/
rake_test_loader.rb" "test/functional/user_controller_test.rb"
Loaded suite c:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader

K:/development/ruby/rails/contact/test/mocks/test/time.rb:10:in `now_old': stack
 level too deep (SystemStackError)

Here's test/mocks/test/time.rb

require 'time'

Time.class_eval { 
  @@advance_by_days = 0
  cattr_accessor :advance_by_days

  class << Time
    alias now_old now
    def now
      if Time.advance_by_days != 0
        return Time.at(now_old.to_i + Time.advance_by_days * 60 * 60 * 24 + 1)
      else
        now_old
      end
    end
  end
}

I haven't got my head round all of Ruby's metaclass abilities yet but I think alias isn't behaving as advertised. I
replaced the implementation with the following which seems to work:

require 'time'

Time.class_eval { 
  @@advance_by_days = 0
  cattr_accessor :advance_by_days

  class << Time
    old_now = self.instance_method(:now)
    
    define_method(:now) do
      tmp = old_now.bind(self).call
      if Time.advance_by_days != 0
        return Time.at(tmp.to_i + Time.advance_by_days * 60 * 60 * 24 + 1)
      else
        tmp
      end
    end
  end  
}

Add A Comment: Notepad

Please login


Followup

No Followups Have Been Posted

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item