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 6610777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:53:03+00:00 2026-05-25T19:53:03+00:00

They say the perfect test just includes the test framework and the class being

  • 0

They say the perfect test just includes the test framework and the class being tested; everything else should be mocked. Then, what about associations?

And I don’t mean simple has_many, belongs_to associations, but association extensions and scopes. I’d really like to write specs for scopes, but I just can’t figure how to do it.

  • 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-25T19:53:03+00:00Added an answer on May 25, 2026 at 7:53 pm

    I got caught up on this too. In Rspec, they get rid of the idea of “Unit Tests”. In practice, unit tests in Rails means, at least to me, testing the attribute values on a model. But you’re right, what about associations?

    In Rspec you just create a spec/models directory, and test your models. At the top of a model spec (spec/models/user_spec.rb), you have your unit tests (testing your attributes), then you test each association below:

    require 'spec_helper'
    
    describe User do
      context ":name" do
        it "should have a first name"
        it "should have a last name"
      end
    
      # all tests related to the gender attribute
      context ":gender" do
        it "should validate gender"
      end
    
      # all tests related to "belongs_to :location"
      context ":location" do
        it "should :belong_to a location"
        it "should validate the location"
      end
    
      # all tests related to "has_many :posts"
      context ":posts" do
        it "should be able to create a post"
        it "should be able to create several posts"
        it "should be able to list most recent posts"
      end
    end
    

    But now you’re testing the Post and Location models in your User tests? Yep. But the Post model is going to have a bunch of extra stuff outside of what it does in relation to the user. Same with Location. So you’d have a spec/models/location_spec.rb like:

    require 'spec_helper'
    
    describe Location do
      context ":city" do
        it "should have a valid city"
      end
    
      context ":geo" do
        it "should validate geo coordinates"
      end
    end
    

    None of that should be mocked in my opinion. At some point you have to actually test the associations are saving and are queryable. That’s here. Think about it as, in the model specs, you have “Unit Test” for attributes and “Integration Tests” for associations.

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

Sidebar

Related Questions

when they say the action controller in the struts framework is multi threaded, does
In java they say don't concatenate Strings, instead you should make a stringbuffer and
Im learning Mockito and in chapter 16 they say you should not use partial
Many people have argued about function size. They say that functions in general should
They say Cookies are bad . I personally believe there should be a "smarter"
whenever they say run a .class file in JVM. do they mean with the
They say a single image is worth 1000 words: I'll just note that the
when they say we will use unix time, does that mean i should create
Whenever I read about Swing they say they are light weight components. So I
I'm really beginning to understand what people mean when they say that C++'s error

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.