Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking ActiveRecord Associations in RSpec

I am learning how to write test cases using Rspec. I have a simple Post Comments Scaffold where a Post can have many Comments. I am testing this using Rspec. How should i go about checking for Post :has_many :comments. Should I stub Post.comments method and then check this with by returning a mock object of array of comment objects? Is testing for AR associations really required ?

like image 786
Alok Swain Avatar asked Apr 20 '10 06:04

Alok Swain


People also ask

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I test a controller in Ruby on Rails?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

Is RSpec TDD or BDD?

rspec is a BDD test suite. Unlike Test::Unit, rspec isn't built into the language's Standard Library, which means if you want to use this tool, you'll need to install an external program (these programs are called ruby gems) to your application before you can start using it.


2 Answers

Since ActiveRecord associations should be well-tested by the Rails test suite (and they are), most people don't feel the need to make sure they work -- it's just assumed that they will.

If you want to make sure that your model is using those associations, that's something different, and you're not wrong for wanting to test that. I like to do this using the shoulda gem. It lets you do neat things like this:

describe Post do
  it { should have_many(:comments).dependent(:destroy) }
end
like image 57
Robert Speicher Avatar answered Oct 12 '22 03:10

Robert Speicher


Testing associations is good practice generally, especially in an environment where TDD is highly regarded- other developers will often look to your specs before looking at the corresponding code. Testing associations makes sure that your spec file most accurately reflects your code.

Two ways you can test associations:

  1. With FactoryGirl:

    expect { FactoryGirl.create(:post).comments }.to_not raise_error
    

    This is a relatively superficial test that will, with a factory like:

    factory :post do
      title { "Top 10 Reasons why Antelope are Nosy Creatures" }
    end
    

    return you a NoMethodError if your model lacks a has_many association with comments.

  2. You can use the ActiveRecord #reflect_on_association method to take a more in-depth look at your association. For instance, with a more complex association:

    class Post
      has_many :comments, through: :user_comments, source: :commentary
    end
    

    You can take a deeper look into your association:

    reflection = Post.reflect_on_association(:comment)
    reflection.macro.should eq :has_many
    reflection.options[:through].should eq :user_comments
    reflection.options[:source].should eq :commentary
    

    and test on whatever options or conditions are relevant.

like image 42
mhriess Avatar answered Oct 12 '22 04:10

mhriess