Created
March 27, 2009 01:03
-
-
Save ohokay/86481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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