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
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. On 5/4/06, Pat Maddox <pergesu@gmail.com> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Dan Venkitachalam expandingbrain.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060504/c6a3d0d9/attachment.html
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 On 5/4/06, Dan Venkitachalam <dan@expandingbrain.com> wrote:> 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. > > > On 5/4/06, Pat Maddox <pergesu@gmail.com> wrote: > > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > Dan Venkitachalam > expandingbrain.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >