In some of my unit tests, I find the need to set today to a different
value than the actual today.  For example, to test the model method
"balance_of_today", which takes no argument, I want to have Date.today
be a specific date for the purpose of testing. In other words, I want
to do something like this:

mock_today(Date.new(2008, 2, 1)) do
    ... test code...
end

My implementation for mock_today is:

  def mock_today(date)
    $_mock_test_date = date

    Date.class_eval do
      class <<self
        alias original_today today
      end

      def self.today
        $_mock_test_date
      end
    end

    yield

    Date.class_eval do
      class <<self
        alias today original_today
      end
    end
  end

So my questions are:

 1) Is it a good idea to mock Date.today?

 2) Can my implementation of mock_today be improved?  I'm particularly
worried about of the use of global variable, and of class_eval, since
I heard class_eval is becoming outdated as Ruby 1.9 is coming out.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to