I’ve been working on a very large Rails projects at work and found myself spending a lot of time writing the same unit tests over and over again. The application has a very complex database schema and has 159 ActiveRecord models so far. With this many models, there are obviously a ton of associations. I decided that I needed a way to automatically test every association that is defined in a model.
The TestInjector plugin is the result. I still have a lot of functionality that I want to add, but it works very well and it’s ready for others to use.
I posted the details on the Rails wiki:
http://wiki.rubyonrails.org/rails/pages/TestInjector
Or just install it using the plugin manager and take a look at the README file for more details:
script/plugin install http://www.infused.org/svn/plugins/test_injector
or:
script/plugin discover script/plugin install test_injector



There are a few plugins that add associations to model classes. In my own tests I don’t want to test these plugins and I really don’t want to go to the trouble of setting up the fixtures for them. So I need a way to ignore associations in addition to those that TestInjector already knows about. It’s a pretty simple change to test_injector.rb
@@ignored_associations = [:versions, :parent, :children]
mattr_accessor :ignored_associations
…
def inject_association_tests
collectible_associations = [:has_many, :has_and_belongs_to_many]
klass.reflect_on_all_associations.each do |association|
unless TestInjector.ignored_associations.include?(association.name)
…
Good suggestion Michael. The latest version lets you define the ignored associations in the inject_test declaration:
inject_tests :activerecord, :ignore_associations => [:groups, :tokens]
To avoid necessity to have associations for EACH object,
suggest to apply the rule only for the FIRST object.
Small change in the test_injector.rb define_collectible_association_test method:
record = klass.find(:first, :conditions => “id=1″)
Does this plugin work on Rails 2? I’m trying use it but not having much luck. It looks like Rails 2 uses ActiveSupport::TestCase as opposed to Test::Unit::TestCase. I changed that in my local copy, but I’m still seeing undefined method inject_activerecord_tests. Any thoughts?