views:

36

answers:

3

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!!

+3  A: 

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.

Beerlington
@Beerlington, are there any common things you personally do when testing? I'm looking for baseline things i should do right off the bat when getting started. Things like... testing my associations seems reasonable but should i then test every method through the association? or how does one know when to stop?! lol
Zaz
I really like using these quick one-line tests because they are incredibly easy to setup. I always start with these and add every relationship and validation, including all the through associations. It doesn't take much work and doesn't add much overhead to your tests. Then as I add functionality, I will add more unit tests. If you write the tests for your models as you're writing the code, it really forces you to write simple, modular code.
Beerlington
+2  A: 

Don't forget RSpec and Cucumber. Two really great Behaviour-Driven Development tools.

Mike Bethany
A: 

remarkable will do this nicely:

describe Pricing do

  should_have_many :accounts, :through => :account_pricings
  should_have_many :account_pricings
  should_have_many :job_profiles, :through => :job_profile_pricings
  should_have_many :job_profile_pricings

end

Generally, you just copy all of your relationships from the model to the spec and change "has" to "should_have", "belongs_to" to "should_belong_to", and so on. To answer the charge that it's testing rails, it checks the database also, making sure that the association works.

Macros are also included for checking validations as well:

should_validate_numericality_of :amount, :greater_than_or_equal_to => 0
Wayne Conrad
remarkable sounds cool! Are you using it with a rails3 env ?
Zaz
@Zaz, I've used it with Rails 2.2.3 and 2.3.9 so far. I believe it works with Rails 3 as well, but I haven't tried it.
Wayne Conrad