Validating your Fixtures

by Trevor Turk

Here’s a easy way to ensure that you’re using valid data in your fixtures with Rails and Test::Unit:

# test/integration/fixture_validation_test.rb
require 'test_helper'

class FixtureValidationTest < ActionController::IntegrationTest

  test "fixtures should be valid" do
    models = Fixtures.all_loaded_fixtures.keys
    models.each do |model|
      model = model.camelize.singularize.constantize
      fixtures = model.find(:all)
      fixtures.each do |fixture|
        if !fixture.valid?
          puts; puts "WARNING: Invalid fixture: #{fixture.inspect}"
        end
        assert_valid fixture
      end
    end
  end
end

Please feel free to suggest any improvements. Thanks!

Advertisement