Testing has_many

I have been developing with RoR for a little but haven't really dived too much into the testing framework yet ( I know, sacrilege!) . I was wondering about the best way to test a has_many relationship. Let's say I have an album that contains many photos. The album model has_many :photos while the photo model belongs_to :album. I have the album unit test working but was interested in a way to test that the relationship will work how I want it to. Should I put that code in the album unit test? In the functional test? Or in one of the Photo tests?

p_W wrote:

I have been developing with RoR for a little but haven't really dived too much into the testing framework yet ( I know, sacrilege!) . I was wondering about the best way to test a has_many relationship. Let's say I have an album that contains many photos. The album model has_many :photos while the photo model belongs_to :album. I have the album unit test working but was interested in a way to test that the relationship will work how I want it to.

ActiveRecord is well tested. Focus on testing your app code instead. (reflect_on_association is handy for testing that you've specified your associations properly.)

Should I put that code in the album unit test?

I tend to put my association specs in the class unit spec (I use RSpec, not Test::Unit), since associations are a property of the model. I'd usually test each end of the association in the respective model's spec file.

In the functional test?

Forget those things and go straight to Cucumber.

Best,

While testing your models, you do not have to test Rails's functionality. If there is nothing else in the model apart from has_many and belongs_to statements, you can safely ignore unit testing those models entirely. However, if you have validations and custom methods on the model, you would have to test the functionality of that method in your unit tests.