Reputation: 342
With RSpec and Shoulda you can:
it { should belong_to(:product) }
I am told specs should specify observed behavior. This spec also does seem like duplication of code that can be written in the model. So is there a time and place to actually use a test like this?
Upvotes: 1
Views: 51
Reputation: 11647
The bigger question is, why is it bad to test this? If you are told specs should specify observed behaviour, and a model having a belongs_to
automatically gives it a method to access the association, is that not something to observe? You could test the #product
method instead, but how would that test go?
it "has an association to a product" do
product = Product.create
model = Model.create(:product_id => product.id)
model.product.should eq product
end
Is that really better than just using the single liner?
it { should belong_to(:product) }
If the code is in any way important, you should test it.
Furthermore, if you were following TDD, you would write the test first to specify that an association has to be there, then put in the code to maintain that test.
Upvotes: 3