Note: You are viewing a non-styled version of this website. Click here to skip to the content.

Vermonster - Open Standards through Open Source

Vermonster Logo

Archive for the RSpec Category

RSpec-ing Model Relationships

Posted on July 15th, 2008 under Code, RSpec, Ruby on Rails.

If you are into the BDD (Behavior Driven Development) and Ruby RSpec, you should really check out this association matcher we came across. We’ve had good luck with it and use it in all our new applications now. It is a really concise way to define your model relationships.

1
2
3
4
5
6
7
8
9
describe Order, " (relationships)" do
  it "should belong_to a user" do
    Order.should belong_to(:user)
  end
 
  it "should belong_to a source with a polymorphic relationship" do
    Order.should belong_to(:source).polymorphic
  end
end

Could be the specs for this code:

1
2
3
4
5
class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :source, :polymorphic => :true
  # ...
end