Testing fields

Hi,

I have a model named ‘applicant’.
Applicant has various fields, one of them is ‘date_of_birth’
I have split the field ‘date of birth’ into three fields:
‘dob_day’, ‘dob_month’, ‘dob_year’

From the view:
<% form_for :applicant, :url=> {:action => “index”} do |f| %>

  • <%= f.label :dob, "Date of birth" %> <%= f.text_field :dob_day, :maxlength => 2 %> <%= f.text_field :dob_month, :maxlength => 2 %> <%= f.text_field :dob_year, :maxlength => 4 %> <%= error_message_on :applicant, :date_of_birth %>
<% end %>

The idea is that the user then types their date of birth into the text
fields in the form DD-MM-YYYY.
As these three fields (‘dob_day’, ‘dob_month’, ‘dob_year’) don’t exist
in the database (only ‘date_of_birth’) does, Rails then looks in the
model, where it finds the following getter and setter methods:

def dob_day=(day)
@dob_day = day
make_date
@dob_day
end

def dob_month=(month)
@dob_month = month
make_date
@dob_month
end

def dob_year=(year)
@dob_year = year
make_date
@dob_year
end

def make_date
if defined? @dob_day and @dob_day != “” and defined? @dob_month and
@dob_month != “” and defined? @dob_year and @dob_year !=""
self.dob = [@dob_day, @dob_month, @dob_year].join(’-’)
else
self.dob = “”
end
end

This has the effect that a string in the form of “01-01-2009” is
returned to the field ‘date_of_birth’

This works great.

My question: how would I write a test to check that if I set ‘dob_day’,
‘dob_month’, ‘dob_year’ to ‘01’, ‘01’, ‘2000’, that the strig
‘01-01-2000’ is returned?

I tried this:

def test_should_return_date_of_birth
applicant = Applicant.new(valid_applicant_attributes(:date_of_birth =>
“”))
date_of_birth_day = “01”
date_of_birth_month = “01”
date_of_birth_year = “1999”
assert_equal “01-01-1999”, applicant.date_of_birth
end

but got the error:
(“01-01-1999”) expected, but was ("").

And this:

def test_should_return_date_of_birth
applicant = Applicant.new(valid_applicant_attributes(:date_of_birth =>
“”, :date_of_birth_day => “01”, :date_of_birth_month => “01”,
:date_of_birth_year => “1999”))
assert_equal “01-01-09”, applicant.date_of_birth
end

but got the error:
unknown attribute: date_of_birth_year

Sorry if this question is long winded, I lack the terminology to explain
it any other way (suggestions welcome).

Thanks for any help you can give.

Probably doesn’t make any difference to your understanding, but I forgot
to mention that in my model is also:

attr_reader :dob_day, :dob_month, :dob_year

2009/5/25 Jim B. [email protected]

    The idea is that the user then types their date of birth into the text

    end
    This has the effect that a string in the form of “01-01-2009” is
    def test_should_return_date_of_birth
    applicant = Applicant.new(valid_applicant_attributes(:date_of_birth =>
    “”))
    date_of_birth_day = “01”
    date_of_birth_month = “01”
    date_of_birth_year = “1999”
    assert_equal “01-01-1999”, applicant.date_of_birth
    end

Assuming this is a unit test then I think it should be
application.dob_day = “01” etc.

assert_equal “01-01-09”, applicant.date_of_birth
end

but got the error:
unknown attribute: date_of_birth_year

Again the attriutes are dob_day etc not date_of_birth_day.

Assuming this is a unit test then I think it should be
application.dob_day = “01” etc.
Again the attriutes are dob_day etc not date_of_birth_day.

Thanks for that Colin,

def test_should_make_date
applicant = Applicant.new(valid_applicant_attributes(:date_of_birth =>
“”))
applicant.dob_day = “01”
applicant.dob_month = “01”
applicant.dob_year = “1999”
assert_equal “01-01-1999”, applicant.date_of_birth
end

works (as you might expect) absolutely fine.

It’s been a long day already, I think I need some more coffee.
Thanks for taking the time to read through my question and find my typo.

Jim