Ruby-on-rails 如何将 db:seed 数据自动加载到测试数据库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1574797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:52:02  来源:igfitidea点击:

How to load db:seed data into test database automatically?

ruby-on-railstestingseed

提问by Luke Francl

I'm attempting to use the new standard way of loading seed data in Rails 2.3.4+, the db:seedrake task.

我正在尝试使用在 Rails 2.3.4+ 中加载种子数据的新标准方式,即db:seedrake 任务。

I'm loading constant data, which is required for my application to really function correctly.

我正在加载常量数据,这是我的应用程序真正正常运行所必需的。

What's the best way to get the db:seedtask to run before the tests, so the data is pre-populated?

db:seed在测试之前运行任务以便预先填充数据的最佳方法是什么?

采纳答案by ryanb

The db:seedrake task primarily just loads the db/seeds.rbscript. Therefore just execute that file to load the data.

db:seedrake任务主要只是用来加载db/seeds.rb脚本。因此只需执行该文件即可加载数据。

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

# or

Rails.application.load_seed

Where to place that depends on what testing framework you are using and whether you want it to be loaded before every test or just once at the beginning. You could put it in a setupcall or in a test_helper.rbfile.

放在哪里取决于您使用的测试框架以及您是希望在每次测试之前加载它还是在开始时加载一次。你可以把它放在一个setup电话或一个test_helper.rb文件中。

回答by Eugene Bolshakov

I'd say it should be

我会说应该是

namespace :db do
  namespace :test do
    task :prepare => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end

Because db:test:load is not executed if you have config.active_record.schema_format = :sql (db:test:clone_structure is)

因为 db:test:load 如果你有 config.active_record.schema_format = :sql (db:test:clone_structure is) 就不会执行

回答by Nick M

Putting something like this in lib/tasks/test_seed.rake should invoke the seed task after db:test:load:

把这样的东西放在 lib/tasks/test_seed.rake 应该在 db:test:load 之后调用种子任务:

namespace :db do
  namespace :test do
    task :load => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end

回答by Matt

I believe Steve's commentabove should be the correct answer. You can use Rails.application.load_seedto load seed data into your test envtheitroadonment. However, when and how often this data is loaded depends on a few things:

我相信史蒂夫上面的评论应该是正确的答案。您可以使用Rails.application.load_seed将种子数据加载到测试环境中。但是,加载这些数据的时间和频率取决于以下几点:

Using Minitest

使用 Minitest

There is no convenient way to run this file once before all tests (see this Github issue). You'll need to load the data once before each test, likely in the setup method of your test files:

在所有测试之前没有方便的方法运行此文件(请参阅此 Github 问题)。您需要在每次测试之前加载一次数据,可能在测试文件的设置方法中:

# test/models/my_model_test.rb
class LevelTest < ActiveSupport::TestCase

  def setup
    Rails.application.load_seed
  end

  # tests here...

end

Using RSpec

使用 RSpec

Use RSpec's before(:all)method to load seed data for all test for this model:

使用 RSpec 的before(:all)方法为该模型的所有测试加载种子数据:

describe MyModel do
  before(:all) do
  Rails.application.load_seed
end

describe "my model..." do
  # your tests here
end

Hope this helps.

希望这可以帮助。

回答by Gary S. Weaver

For those using seedbank, it changes how seeds are loaded, so you probably can't/don't want to use the load ...solution provided here.

对于使用种子库的用户,它会改变种子的加载方式,因此您可能无法/不想使用load ...此处提供的解决方案。

And just putting Rake::Task['db:seed'].invokeinto test_helper resulted in:

只是放入Rake::Task['db:seed'].invoketest_helper 导致:

Don't know how to build task 'db:seed' (RuntimeError)

But when we added load_tasks before that, it worked:

但是当我们在此之前添加 load_tasks 时,它起作用了:

MyApp::Application.load_tasks
Rake::Task['db:seed'].invoke

回答by jondahl

We're invoking db:seed as a part of db:test:prepare, with:

我们调用 db:seed 作为 db:test:prepare 的一部分,使用:

Rake::Task["db:seed"].invoke

Rake::Task["db:seed"].invoke

That way, the seed data is loaded once for the entire test run, and not once per test class.

这样,种子数据在整个测试运行中加载一次,而不是每个测试类加载一次。

回答by alexpls

Adding Rake::Task["db:seed"].invoketo the db:test:preparerake task did not work for me. If I prepared the database with rake db:test:prepare, and then entered the console within the test environment, all my seeds were there. However, the seeds did not persist between my tests.

添加Rake::Task["db:seed"].invokedb:test:preparerake 任务对我不起作用。如果我用 准备数据库rake db:test:prepare,然后在测试环境中进入控制台,我所有的种子都在那里。然而,这些种子在我的测试之间并没有持续存在。

Adding load "#{Rails.root}/db/seeds.rb"to my setup method worked fine, though.

不过,添加load "#{Rails.root}/db/seeds.rb"到我的设置方法中效果很好。

I would love to get these seeds to load automatically and persist, but I haven't found a way to do that yet!

我很想让这些种子自动加载并持续存在,但我还没有找到一种方法来做到这一点!

回答by Mark Schneider

Building on Matt's answer, if taking that sort of route, I recommend calling Rails.application.load_seedin a before(:suite)block in rspec_helper.rbrather than in a before(:all)block in any file. That way the seeding code is invoked only once for the entire test suite rather than once for each group of tests.

基于 Matt 的回答,如果采用这种方式,我建议调用Rails.application.load_seedinbefore(:suite)rspec_helper.rb而不是before(:all)任何文件中的块。这样,播种代码只为整个测试套件调用一次,而不是为每组测试调用一次。

rspec_helper.rb:

rspec_helper.rb:

RSpec.configure do |config|
  ...
  config.before(:suite) do
    Rails.application.load_seed
  end
  ...
end