assert fixtures are valid

put this in your test_helper.rb


  def test_all_models_valid model
    objs = model.camelize.constantize.find(:all)
    objs.each do |obj|
      assert obj.valid?, "#{model} #{obj.id} is invalid: #{obj.errors.full_messages.join(', ')}" 
    end
  end

  def test_all_models_valid! filename
    test_all_models_valid klass = filename.split('/').last.sub(/_test.rb/,'').camelize
  end

Now you can create a test like this in each of your your model/unit tests:

  def test_valid_fixtures
    test_all_models_valid! __FILE__
  end

Sweet! :)

Update #1: Thanks to Nicolas Sanguinetti for the tip on how to avoid the use of eval.

1 Response to “assert fixtures are valid”

  1. Nicolás Sanguinetti Says:

    Hmmm... why use eval? model.camelize.constantize.find(:all) it's much nicer imo :)

Leave a Reply