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,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
On Tue, Jul 7, 2009 at 8:06 AM, p_W[email protected] 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. Â Should I put that code in
the album unit test? In the functional test? Â Or in one of the Photo
tests?
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.
–
Regards,
Vagmi Mudumbai
CTO & Co Founder
Artha42 Technology Solutions Pvt. Ltd.,
http://www.artha42.com
Rails functionality is well tested. But if you want to test I shal
explain based on thoughtbot shoulda which I am using There are several
macros given by shoulda
In your case in test/unit/album_test.rb you can write
should_have_many :photos
In test/unit/photo_test.rb
should_belong_to :album
Sijo