Fixture Validations in Using Shoulda
2008-10-19
I was working on my family budget application and wanted to validate existing fixtures. I wrote the following:
test/shoulda_macros/validation_macros.rb
1 module ValidationMacros 2 def self.included(base) 3 base.send :extend, ClassMethods 4 end 5 6 module ClassMethods 7 def should_have_valid_fixtures(klass=self) 8 should "have all valid fixtures" do 9 klass.name.sub("Test", "").constantize.all.each do |object| 10 assert object.valid?, "Fixture #{object.inspect} is invalid" 11 end 12 end 13 end 14 end 15 end 16 17 Test::Unit::TestCase.send :include, ValidationMacros 18 19 # Usage Example 20 class AccountTest < Test::Unit::TestCase 21 should_have_valid_fixtures 22 end 23 24 class StrangeNameTest < Test::Unit::TestCase 25 # Pass the ActiveRecord (or anything that respond_to?(:valid?) 26 # and respond_to?(:all)) class to validate against. 27 should_have_valid_fixtures Account 28 end
This might be interesting if you use fixtures.
blog comments powered by Disqus