Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:48:48+00:00 2026-05-16T04:48:48+00:00

Should I write unit tests for my associations? I haven’t found many good resources

  • 0

Should I write unit tests for my associations?

I haven’t found many good resources on how and whether to do the testing. I also see/hear some opinion that it is okay to not test your associations (belongs_to and has_many) as they are already tested in rails. And there is another view that says, if it code you write, it is code you test.

So if you say I should, please tell me few good ways of doing this.
Currently, I’m writing tests using Test::Unit and I’m not using Shoulda (I don’t have any macros). So for testing each association, I am creating a bunch of objects and then doing asserts on them. Somewhat like this –

For a Post model that has_many comments, my test logic goes this way –

p = Post.create(:title => 'dummy_title', :content => 'lorem ...')
3.times{ Comment.create(:post_id :=> p.id, :commentor => 'jack')}
assert_equal(3, p.comments.size, "post doesn't have correct no of comments")

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:48:49+00:00Added an answer on May 16, 2026 at 4:48 am

    To me, testing the association borders on testing the language (or in this case, testing the framework).

    I’d reserve it for circumstances where you’re doing something nonstandard with the association. For example, if every time you create a comment it changes something in the parent post, test that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen people write unit tests for things that should simply be taken for
I have a task to write a set of http unit tests, which should
I am wondering should I write unit test for everything. There are some classes
What criteria should I use to decide whether I write VBA code like this:
I want to write a small program that should print something like testing CPU...
I'm relatively new to unit testing, and I've discovered that while many sources say
I am interested in the best way to write unit tests for a class
I want to write unit tests for a web service. I create my test
I have been using MSpec to write my unit tests and really prefer the
I want to write some unit tests for an interceptor that intercepts the Loggable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.