DateTime.new(2006, 1, 23, 13, 14, 15).
This can be particularly important if you are working with code similar to this:
class CommentIn the example, you would like a test that proves that the created_at instance variable is set in the constructor, such as:
@created_at = DateTime.now
class CommentTest < Test::Unit::TestCaseUnfortunately, this test doesn't work correctly if the DateTime instances aren't created at exactly the same point in time.
assert_equal DateTime.now, Comment.new.created_at
To solve this problem the Environment class was designed to allow you to specify a date in the expected results of a test without being concerned if it matched the current date or not. The Environment solution worked, but it wasn't particularly elegant.
We decided to refactor to what we thought was a simpler solution. We added the following code directly to the test class.
class << DateTimeBy redefining the now method we were able to reliably assert equality. This change removed the need for the Environment class also. Of course, DateTime.now will always return this date for all the tests that follow this one; we were comfortable with all the tests depending on this constant value.
DateTime.new(2006, 1, 23, 13, 14, 15)