Skip to content

Instantly share code, notes, and snippets.

@danini-the-panini
Last active August 7, 2020 15:24
Show Gist options
  • Save danini-the-panini/0fc858c67a19e57b39d833c12b8244b1 to your computer and use it in GitHub Desktop.
Save danini-the-panini/0fc858c67a19e57b39d833c12b8244b1 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Smith revised this gist Aug 7, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions fixture_test.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # Test validity of all your fixtures in one simple test.
    # Just put this file somewhere, like maybe `test/models`, and run `bin/rails test test/models/fixture_test.rb`.
    # Even gives you the line number of the fixture definition in the failure message!

    require 'test_helper'

    class FixturesTest < ActiveSupport::TestCase
  2. Daniel Smith revised this gist Aug 7, 2020. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    Test validity of all your fixtures in one simple test.

    Just put this file somewhere, like maybe `test/models`, and run `bin/rails test test/models/fixture_test.rb`.

    Even gives you the line number of the fixture definition in the failure message!
  3. Daniel Smith revised this gist Aug 7, 2020. No changes.
  4. Daniel Smith created this gist Jul 17, 2020.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Test validity of all your fixtures in one simple test.

    Just put this file somewhere, like maybe `test/models`, and run `bin/rails test test/models/fixture_test.rb`.

    Even gives you the line number of the fixture definition in the failure message!
    17 changes: 17 additions & 0 deletions fixture_test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    require 'test_helper'

    class FixturesTest < ActiveSupport::TestCase
    Dir.glob(Rails.root.join('test', 'fixtures', '*.yml')).each do |filename|
    set_name = File.basename(filename, '.yml')

    test "#{set_name} fixtures are valid" do
    ActiveRecord::FixtureSet.all_loaded_fixtures[set_name].each do |fixture_name, fixture|
    value = fixture.find
    unless value.valid?
    line_number = File.read(filename).each_line.find_index { |l| l.chomp.match?(/\A#{fixture_name}:\z/) } + 1
    fail "#{filename}:#{line_number}: #{value.errors.full_messages.to_sentence}"
    end
    end
    end
    end
    end