Ruby-on-rails: seed treatment strategies (or loading test data into a developer database)

I want to frequently clean and reload the developer database (Ruby on rails).

Of course, I can manually add data through a web page, but I was wondering if anyone had strategies for this type of testing.

(I already have unit, functional and integration tests, fyi)

thank

+3
source share
2 answers

Create a file seed.ymlin the directory db. Add a YAML document for each model you want to create. This document should contain a list of hashes. Each hash must contain model attributes.

  users:
      -   login: jake
          password: jake123
          password_confirmation: jake123
          first_name: Jake
          last_name: Driver

      -   login: Jane
          password: jane123
          password_confirmation: jane123
          first_name: Jane
          last_name: McCain

  categories:

  products:

In seed.rb file

seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
User.transaction do 
  config.keys.each{ |key| key.classify.constantize.create(config[key]) }
end

YML. , , . .

, rake lib\tasks. rake app:flush.

namespace :app do
  desc "Flush all the seed data "
  task :flush => :environment do
    config = YAML::load_file(File.join(Rails.root, 'db', 'seed.yml'))
    User.transaction do 
      config.keys.each{ |table| truncate_table(table)}
    end
  end
end
+6

Source: https://habr.com/ru/post/1735645/


All Articles