# Testing associations

**URL:** <https://www.ruby-forum.com/t/testing-associations/94492>\
**Category:** Rails\
**Created:** [May 5, 2006, 12:55am UTC](https://www.ruby-forum.com/t/testing-associations/94492 "2006-05-05T00:55:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 5, 2006, 12:55am UTC](https://www.ruby-forum.com/t/testing-associations/94492/1 "2006-05-05T00:55:01Z")

</div>

I have a pretty simple model, articles and users.

class Article  
belongs\_to :user  
end

class User  
has\_many :articles  
end

In my unit tests I want to ensure that the associations work properly.  
What’s the best way to do this? The obvious thing to do is a test in  
each model.

# user\_test.rb

def test\_add\_post  
u = users(:first)  
before\_articles = u.articles.count  
u.articles \<\< Article.new(:title =\> “Title”, :body =\> “Body”)  
u.reload  
assert\_equal before\_articles + 1, u.articles.count  
end

# article\_test.rb

def test\_create\_with\_user  
a = Article.new(:title =\> “Title”, :body =\> “Body”, :user =\>  
users(:first))  
assert a.save  
a.reload  
assert\_equal users(:first), a.user  
end

The thing that bugs me is that in my unit test for article I have to  
include the users fixtures. As my domain model gets more complicated,  
my tests require more fixtures. It just feels messy to me. This is a  
pretty basic question, but it’s something that’s bothered me for a  
long time throughout several projects…am I justified in thinking  
it’s ugly, or is that the correct approach?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 5, 2006, 1:22am UTC](https://www.ruby-forum.com/t/testing-associations/94492/2 "2006-05-05T01:22:38Z")

</div>

I wonder if it’s really necessary to test belongs\_to and has\_many  
associations, given they are covered in Rails unit tests. In my own  
projects I find it sufficient to use the associations in unit tests only  
as  
part of the model behaviour. This makes the inclusion of the associated  
fixtures required only when it’s part of the model behaviour.

Cheers,

Dan.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [May 5, 2006, 1:28am UTC](https://www.ruby-forum.com/t/testing-associations/94492/3 "2006-05-05T01:28:52Z")

</div>

One reason to have it is that if for some reason the belongs\_to or  
has\_many statement gets deleted/modified, it gets caught in the unit  
tests. It’s not so much ensuring that they work properly (because as  
you said, the Rails code is thoroughly tested), but that they’re in  
fact set up correctly.

Pat
