Plugins - Test Injector
Add to favoritesTests performed
The automatically injected tests are currently limited to tests against ActiveRecord models. However, there is nothing stopping you from adding tests for other portions of the framework.
ActiveRecord tests
Every association defined in the ActiveRecord model. A common model might look like:
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
belongs_to :company
has_many :purchases, :class_name => "Order", :dependent => :nullify
has_many :subscriptions, :dependent => :destroy
has_many :newspapers, :through => subscriptions
end
By calling inject_activerecord_tests in the class definition, several test will be injected into the test suite at runtime.
class UserTest < Test::Unit::TestCase
fixtures :groups, :companies, :orders, :subscriptions, :newspapers
inject_tests :activerecord
end
Based on the example User class, the following tests will be run (notice that we didn’t have to define any test methods in the test class):
def test_has_and_belongs_to_many_groups
# asserts that groups is an Array of Groups
end
def test_belongs_to_company
# asserts that company return an instance of Company
end
def test_has_many_purchases
# asserts that purchases is an Array of Purchases
# asserts that associated purchases's user_id column is nullified when the user is deleted
end
def test_has_many_subscriptions
# asserts that subscriptions is an array of Subscriptions
# asserts that all subscriptions are destroyed when a User is deleted
end
def test_has_many_newspapers_through_subscriptions
# asserts that newspapers is an array of Newspapers
end
def test_fixture_defined_for_companies
# tests to see if a fixture called 'companies' is defined in the unit test
end
def test_fixture_defined_for_purchases
# tests to see if a fixture called 'orders' is defined in the unit test
end
def test_fixture_defined_for_subscriptions
# tests to see if a fixture called 'subscriptions' is defined in the unit test
end
def test_fixture_defined_for_newspapers
# tests to see if a fixture called 'newspapers' is defined in the unit test
end
# if the users table has a 'lock_version' column, one test will be defined
def test_optimistic_locking
# forces a StaleObjectError in order to test that optimisic locking is functioning correctly
end
# if the users table is using the acts_as_versioned plugin by technoweenie, one test will be defined
def test_acts_as_versioned
# tests that the versioned table is set up correctly by exercising the versioning system
end
Ignoring associations
You can specify associations that should not be tested when by using the :ignore_associations option:
inject_tests :activerecord, :ignore_associations => [:company, :groups]
Overriding injected tests
As long as you use place the inject_tests declaration at the top of the test class, you can override any of the injected tests by redeclaring a method with the same name. See above for the test method naming.
To do
- Better tests on polymorphic associations
- Test standard validations
http://www.infused.org/svn/plugins/test_injector/
http://www.infused.org/svn/plugins/test_injector/
Rails' (MIT)
Testing

I played around with this for a while. It doesn't quite work as documented (inject_tests :activerecord doesn't even exist) but those issues were worked around fairly easily. Unfortunately, after I got it all working I found that the automatic fixture selection aspect of this test framework made my tests too fragile so I took them out. I think you're on to something, and look forward to future releases.