Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails RSpec Tests for a has_many :through Relationship

I'm new to testing and rails but i'm trying to get my TDD process down properly.

I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose).

For example, i find that in my model specs i'm definitely writing simple tests to check both ends of a relationship for relating methods.

ie:

require 'spec_helper'  describe Post do    before(:each) do     @attr = { :subject => "f00 Post Subject", :content => "8ar Post Body Content" }   end    describe "validations" do   ...       end    describe "categorized posts" do      before(:each) do       @post  = Post.create!(@attr)     end      it "should have a categories method" do       @post.should respond_to(:categories)     end    end  end 

Then in my categories spec i do the inverse test and check for @category.posts

What else am i missing? thanks!!

like image 633
Mario Zigliotto Avatar asked Oct 19 '10 23:10

Mario Zigliotto


People also ask

Do RSpec tests run in parallel?

Run RSpec Tests in Parallel This is “Parallel Testing”. Parallel Testing gives you the same benefits as running a multi-threaded application and helps you reduce the run time of your test suite, resulting in faster build times and faster releases.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

How do I run a test in RSpec?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.


2 Answers

I would recommend checking out a gem called Shoulda. It has a lot of macros for testing things like relationships and validations.

If all you want is to test that the has_many relationship exists, then you could do the following:

describe Post do   it { should have_many(:categories) } end 

Or if you're testing a has_many :through, then you'd use this:

describe Post do   it { should have_many(:categories).through(:other_model) } end 

I find the Shoulda Rdoc page very helpful too.

like image 79
Peter Brown Avatar answered Oct 05 '22 11:10

Peter Brown


For the sake of completeness, in 2020 this is possible without additional gems.

  it "has many categories" do     should respond_to(:categories)   end 

And even more explicit:

it "belongs to category" do   t = Post.reflect_on_association(:category)   expect(t.macro).to eq(:belongs_to) end 

(see Ruby API on Reflection)

The second example makes sure that a "has_one" is not confused with a "belongs_to" and vice versa

It is, however, not limited to has_many :through relationships, but can be used for any association applied to the model.

(Note: This is using the new rspec 2.11 syntax with Rails 5.2.4)

like image 34
randmin Avatar answered Oct 05 '22 12:10

randmin