2009/5/25 Jim Burgess <rails-mailing-l...@andreas-s.net>

>
> 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| %>
> <ul>
>  <li>
>    <%= 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 %>
>  </li>
> </ul>
> <% 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
>

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


>
> 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
>

Again the attriutes are dob_day etc not date_of_birth_day.


>
> 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.
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to