# Unit testing validates\_associated and HABTM relationships

**URL:** <https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282>\
**Category:** Rails\
**Created:** [December 4, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282 "2006-12-04T19:52:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/1 "2006-12-04T19:52:21Z")

</div>

I’m trying to set up some unit tests and they are failing on just  
basic tests on one model.

The failure is:

1. Failure:  
test\_invalid\_with\_empty\_attributes(WorkAreaTaskTest)  
[test/unit/work\_area\_task\_test.rb:15]:  
 is not true.

My model is:

class WorkAreaTask \< ActiveRecord::Base

belongs\_to :task  
belongs\_to :work\_area  
has\_many :employee\_tasks, :dependent =\> :destroy

acts\_as\_list :scope =\> :work\_area

validates\_presence\_of :task\_id  
validates\_presence\_of :work\_area\_id

validates\_presence\_of :task  
validates\_associated :task

end

And my unit test is:

class WorkAreaTaskTest \< Test::Unit::TestCase  
fixtures :work\_area\_tasks, :employee\_type\_tasks, :tasks, :work\_areas

# Replace this with your real tests.

def test\_truth  
assert true  
end

def test\_invalid\_with\_empty\_attributes  
work\_area\_task = WorkAreaTask.new

```
 assert !work_area_task.valid?
 assert work_area_task.errors.invalid?(:task_id)
 assert work_area_task.errors.invalid?(:work_area_id)
 assert work_area_task.errors.invalid?(:task)

```

end  
end

The failure occurs at the assert invalid tests.

–  
Sterling A.  
sterling [at] [sterlinganderson.net](http://sterlinganderson.net)  
[http://sterlinganderson.net/](http://sterlinganderson.net/)

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/2 "2006-12-04T19:58:30Z")

</div>

Can’t tell from your posting which line is line 15. Where exactly is it  
failing?

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/3 "2006-12-04T20:36:43Z")

</div>

I don’t know why you are checking the id’s. The id’s are usually keys  
that  
are generated automatically when you save the model. Is there any reason  
why  
you are validating id’s?

If you cannot save the model, the id fields will be blank.

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/4 "2006-12-04T20:02:32Z")

</div>

It’s failing on this line:

assert work\_area\_task.errors.invalid?(:task\_id)

The only way to get it to not fail is to comment that line and the  
next 2 as well. So it’s like is getting the the IDs and the task from  
somewhere, because they should be empty.

On 12/4/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. Failure:  
> > test\_invalid\_with\_empty\_attributes(WorkAreaTaskTest)  
> > [test/unit/work\_area\_task\_test.rb:15]:  
> > is not true.
> 
> >

–  
Sterling A.  
sterling [at] [sterlinganderson.net](http://sterlinganderson.net)  
[http://sterlinganderson.net/](http://sterlinganderson.net/)  
608.239.8387

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/5 "2006-12-04T21:33:06Z")

</div>

I’m not checking the id’s from the model I am testing. I am checking  
the foreign key id’s, ie. the id’s from the related tables that need  
to be there for the model to be valid. That’s why I am checking for  
them.

On 12/4/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> >

–  
Sterling A.  
sterling [at] [sterlinganderson.net](http://sterlinganderson.net)  
[http://sterlinganderson.net/](http://sterlinganderson.net/)  
608.239.8387

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/6 "2006-12-04T22:25:18Z")

</div>

Read this first: [Peak Obsession](http://manuals.rubyonrails.com/read/chapter/27)

Test the behavior, not the implementation. You don’t have to check the  
foreign keys. You can save a valid record and read it back and compare  
it  
with fixture data. It is very similar to the example from the above  
tutorial. Remove validates\_presence\_of :task\_id, :work\_area\_id from your  
model class. I would love to be proved wrong here.

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/7 "2006-12-04T23:03:25Z")

</div>

Yes. Also before you start writing tests, jot down specifications in a  
sheet  
of paper. Then write : test\_should\_your\_written\_specification\_one, two  
etc.  
When you do TDD really well you are actually thinking in terms of  
behavior.

If you really want to push the envelope and have some time to kill, then  
check out rSpec. There is a nice Google video that was presented at  
Google  
about rSpec.

For your viewing pleasure:  
[http://video.google.com/videoplay?docid=8135690990081075324&q=bdd&hl=en](http://video.google.com/videoplay?docid=8135690990081075324&q=bdd&hl=en)

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [December 4, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/unit-testing-validates-associated-and-habtm-relationships/78282/8 "2006-12-04T22:51:24Z")

</div>

Ahhh. I see what you are saying. Rooky mistake on my part. 🙂

So basically the belongs\_to is going to take care of the foreign keys,  
so there’s no reason for me to have “validates\_X\_of” for them is what  
you are saying?

On 12/4/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’m not checking the id’s from the model I am testing. I am checking  
> > the foreign key id’s, ie. the id’s from the related tables that need  
> > to be there for the model to be valid. That’s why I am checking for  
> > them.

–  
Sterling A.  
sterling [at] [sterlinganderson.net](http://sterlinganderson.net)  
[http://sterlinganderson.net/](http://sterlinganderson.net/)
