I''m pretty new to Ruby and Rails, but I''ve been dealing w/ TDD
in java for
some time. This works in java (I use it frequently) using interfaces and an
abstract base class for the test classes. Since we don''t have
interfaces or
abstract classes in Ruby, we can actually accomplish the same thing w/o the
interface:
NOTE - none of this code is tested or has even been run
===================#base_commentable_tst.rb (use tst so UnitTest
doesn''t try to run the file)
class BaseCommentableTest < Test::Unit::TestCase
def test_no_comments_when_created
assert_equal(0, commentable.comments.length)
end
def commentable
#override this in derivative test classes
end
===================#excercise_test.rb
class ExcerciseTest < BaseCommentableTest
def commentable
Exercise.new
end
===================#equipment_test.rb
class ExcerciseTest < BaseCommentableTest
def commentable
Equipment.new
end
===================
So you only define the test methods once, but you execute them against each
class that implements those methods. I imagine you could accomplish the same
thing w/ a mixin, but I have yet to dig into them.
Hope this helps,
Cheers,
David
-----Original Message-----
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Richard Livsey
Sent: Monday, July 04, 2005 5:27 PM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Writing tests, how to separate
I''ve got some questions on how you all write and separate up your
tests.
It''s probably going to come down to individual preferences, but
I''ll ask
anyway ;o)
I have a the following basic models:
class Equipment < ActiveRecord::Base
has_many :comments,
:conditions => ["relates_to = ?",
"equipment"],
:foreign_key => "item_id"
end
class Exercise < ActiveRecord::Base
has_many :comments,
:conditions => ["relates_to = ?", "exercise"],
:foreign_key => "item_id"
end
class Comment < ActiveRecord::Base
end
So basically, we have equipment and exercises, both of which can have
comments.
The question is, how to divvy up the comment unit tests?
At the moment I have tests for comments in both exercise and equipment test
classes, along with others in the comments test class.
Initially this felt better as they are more tied to those. However, this
then ends up with duplicate tests (IE check that you can add comments to
exercises, then the same in the equipment tests).
Half of me thinks this is needless duplication and should just be one test
in comments, but the other half says they are separate and test what happens
if one of the other classes changes and only affects that one.
Then there''s the question of do I have the semi-duplicate tests and
have
them all in the comments test class. This does mean that when testing
comments changes I know I only have to run the one test class.
So I guess the question (in a rather roundabout way) is, when you have tests
that are coupled across multiple models, where do you put the tests?
Plus any other tips on testing in rails are always welcome.
--
R.Livsey
http://livsey.org
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails