DateTime validation and testing

Hi!

I have this test:

def test_invalid_date_format
project = Project.new(:name => “Terminar página web”,
:user_id => “1”,
:deadline => “fsafasdfa”)
assert !project.valid?
assert_equal “Deadline has invalid syntax”,
project.errors.on(:deadline)
end

and I have this validation in the model:

protected

def validate
if deadline.nil?
return true
else
begin
dt = DateTime.parse(deadline.to_s)
rescue ArgumentError
errors.add(:deadline, “Deadline has invalid syntax”)
end
end
end

When I execute this test I have this:

  1. Failure:
    test_invalid_date_format(ProjectTest)
    [project_test.rb:47:in test_invalid_date_format' /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/testing/default.rb:7:inrun’]:
    is not true.

How is this possible? Do not Datetime.parse returns false when it is not
a correct datetime string? How do you validate and test DateTime fields?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs