Search code examples
ruby-on-rails-3rspecassociationsscopes

How to write specs for rails associations?


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.


Solution

  • 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.