Skip to content

Instantly share code, notes, and snippets.

@ohokay
Created March 27, 2009 01:03
Show Gist options
  • Save ohokay/86481 to your computer and use it in GitHub Desktop.
Save ohokay/86481 to your computer and use it in GitHub Desktop.
class Test::Unit::TestCase
def self.should_validate_associated(*associations)
klass = self.name.gsub(/Test$/, '').constantize
context "#{klass}" do
associations.each do |association|
should "validate #{association} association" do
assert klass.new.respond_to?("validate_associated_records_for_#{association}")
end
end
end
end
end
### usage example
# model
class Product < ActiveRecord::Base
belongs_to :discount
belongs_to :shop
validates_associated :discount
validates_associated :shop
end
# test
class ProductTest < ActiveSupport::TestCase
should_validate_associated :discount
end
# you test multiple associations just like other shoulda macros
class ProductTest < ActiveSupport::TestCase
should_validate_associated :discount, :shop, :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment