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:
attr_reader :created_at
def initialize
@created_at = DateTime.now
end
end
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.
def test_ctor_inits_created_at
assert_equal DateTime.now, Comment.new.created_at
end
end
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.
def now
DateTime.new(2006, 1, 23, 13, 14, 15)
end
end
Have you seen Stubba? You could do...
ReplyDeleteclass CommentTest < Test::Unit::TestCase
def test_ctor_inits_created_at
now = DateTime.now
DateTime.stubs(:now).returns(now)
assert_equal now, Comment.new.created_at
end
end
- The DateTime class reverts to its original behaviour after each test.
- You can specify different return values in different tests.
- The test is more explicit, because the return value is defined in the test.