I was working on my family budget application and wanted to validate existing fixtures. I wrote the following:

 1 module ValidationMacros
 2   def self.included(base)
 3     base.send :extend, ClassMethods
 4   end
 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
17 Test::Unit::TestCase.send :include, ValidationMacros
19 # Usage Example
20 class AccountTest < Test::Unit::TestCase
21   should_have_valid_fixtures
22 end
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.


Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend


Projects I work on

Projects I worked on