Skip to content

Instantly share code, notes, and snippets.

@alto
Created November 1, 2012 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alto/3991095 to your computer and use it in GitHub Desktop.
Save alto/3991095 to your computer and use it in GitHub Desktop.
fixtures validity
require 'test_helper'
class FixturesTest < ActiveSupport::TestCase
def test_fixtures_for_validity
fixture_tables = all_fixture_tables
assert_not_equal 0, fixture_tables.size, "Unable to find any fixtures in #{fixture_files_path}"
fixture_tables.each do |table_name|
ActiveRecord::Fixtures.all_loaded_fixtures[table_name].each do |f|
fixture_name = f[0]
if fixture_name =~ /^invalid_/
assert !self.send(table_name, fixture_name).valid?, "#{table_name}(:#{fixture_name}) is a valid fixture, but marked as invalid"
else
assert self.send(table_name, fixture_name).valid?, "#{table_name}(:#{fixture_name}) is an invalid fixture"
end
end
end
end
private
def fixture_files_path
File.join(File.dirname(__FILE__) + "/../fixtures", "*.yml")
end
def all_fixture_tables
Dir[fixture_files_path].map { |file| File.basename(file[/^(.+)\.[^.]+?$/, 1]) }
end
end
user:
name: Any User
email: user@my_app.com
user_without_email:
name: User Without Email
invalid_user:
name: Invalid User
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment