# Testing fields

**URL:** <https://www.ruby-forum.com/t/testing-fields/168762>\
**Category:** Rails\
**Created:** [May 25, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/testing-fields/168762 "2009-05-25T14:46:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 25, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/testing-fields/168762/1 "2009-05-25T14:46:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 25, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/testing-fields/168762/2 "2009-05-25T14:48:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 25, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/testing-fields/168762/3 "2009-05-25T15:20:26Z")

</div>

2009/5/25 Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 25, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/testing-fields/168762/4 "2009-05-25T15:28:29Z")

</div>

> 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
