Question

I am using seeds.rb to populate some State model reference data:

State.create :name => 'Alabama', :abbreviation => 'AL'
State.create :name => 'Alaska', :abbreviation => 'AK'
# ...

Although I am not using state fixtures (since it's seed data to begin with, I think it wouldn't be DRY to have to duplicate this purely for tests), the Rails testing framework seems to delete all the State seed data during testing. (I am dropping, recreating, migrating and reseeding the test db, and confirmed the data is there prior to a unit test running.)

The result is this assertion succeeding in seeds.rb but failing in a one-line test:

assert_equal 51, State.all.size

1) Failure:
test_state_seeds_are_present(StateTest) [/test/unit/state_test.rb:24]:
<51> expected but was
<0>.
1 tests, 1 assertions, 1 failures, 0 errors

I have tried both explicitly listing non-State models in the base test class fixtures statement, as well as flipping the transactional fixtures flag (as expected, this only affects records created during the test). Naturally the test under consideration is not itself deleting these records.

The State records are always deleted. Is there a way to tell Rails to just get its hands off the seed data? Do I need to duplicate all the data in fixtures to make sure it gets re-loaded? Short of a major political event, I would expect the state data to be relatively stable.

tia

Was it helpful?

Solution

Tests delete all the data from the database and then load your fixtures (if you have any).

You need to get your test helper to load the seed file before the tests run. There are a couple ways to do that, check out my similar question: How to load db:seed data into test database automatically?

The easiest way is probably just to add

require "#{Rails.root}/db/seeds.rb"

to the top of your test_helper.rb file (assuming you use the built-in testing framework).

OTHER TIPS

The "seed" feature is not integrated into the test architecture. Tests are built around fixtures and each time you run the test suite, Rails loads the data from the fixtures and replaces the existing content.

However, having the database populated with your seed data is really straightforward.

In your test_helper.rb file add a new setup method in the base ActionSupport::TestCase class.

class ActionSupport::TestCase < ...

  setup :load_seeds

  protected 

    def load_seeds
      load "#{Rails.root}/db/seeds.rb"
    end

end

I agree with weppos as to the best way to do this, however for the sake of completeness and for the sake of people who may already have fixture files you can take the other approach and seed your database from the existing fixtures.

This is accomplished by doing something like the following in the db/seeds.rb file

RAILS_FIXTURES = "#{Rails.root}/spec/fixtures"

models_loaded_from_fixtures = %w[Modela Modelb Modelc ....]

models_loaded_from_fixtures.each do |model|
  Fixtures.create_fixtures(RAILS_FIXTURES, "#{model.tableize}")
  puts "Loaded #{model.constantize.all.size} #{model.pluralize}"
end

You could alternatively read the fixures directory and create an array of file names to process, I chose the above process as I wished to be able to specify which of my many existing fixtures I wished to seed the DB with.

I question the need to write a test like that in the first place. Are you testing that the seed data rake task works properly or do you want to test the behavior of your implementation of the State class? I'm assuming the latter, so I would write tests that focus on the behavior and use Factory Girl (or similar) since you mention that you're not using fixtures. Make sure to remove the fixtures :all line from test/test_helper.rb.

To inject a task into the normal flow of running tests, this is an approach that has always worked for me:

namespace :test do
  task :force_environment do
    ENV['RAILS_ENV'] = 'test'
    RAILS_ENV.replace('test')
  end
end

%w(test:units test:functionals test:integration).map do |task_name|
  task = Rake::Task[task_name]
  task.clear_prerequisites

  task.enhance %w(db:test:prepare test:force_environment db:seed)
end

Just put that in a file called lib/tasks/testing.rake and it will get picked up when you next run rake test or other test-related task.

I had the same problem, but the standard answer didn't work for me.

My problem involved places in the class files which did a database lookup for constants and named_scopes. I could not find any way to have the seed data loaded at the right time.

I did find a way to by-pass the delete, which was happening even though I had deleted my fixture file for the missing table.

RAILS_ENV=test rake db:seed ruby -Itest test/units/*.rb

You can't apply both seed and fixtures to setup a same table's data by default.

eg. If you set up a fixtures named products.yml, it will "delete data from products" during creating fixtures.

You can:

  1. Move the seed's stage behind the fixturess(load seed manually);
  2. Use just one of them, or an alternative approach to manage test data;
  3. Don't use both approach to setup a same table's data;
  4. In Rails4.2, you can hack this line in your test_helper.rb like this:

>

require 'rails/test_help'

class ActiveRecord::FixtureSet
  def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}, config = ActiveRecord::Base)
    ...
    # table_rows.each_key do |table|
    #   conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete'
    # end
    ...
  end

This would help in some legacy case, but messing seeds and fixtures up may brings new problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top