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

发布于 2024-08-07 17:30:09 字数 155 浏览 5 评论 0原文

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷默言语 2024-08-14 17:30:09

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

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

# or

Rails.application.load_seed

将其放置在哪里取决于您使用的测试框架以及您是否希望在每次测试之前加载它还是仅在开始时加载一次。您可以将其放入 setup 调用或 test_helper.rb 文件中。

The db:seed rake task primarily just loads the db/seeds.rb script. Therefore just execute that file to load the data.

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 setup call or in a test_helper.rb file.

情丝乱 2024-08-14 17:30:09

我想说应该是

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

因为如果你有 config.active_record.schema_format = :sql (db:test:clone_struct 是),则 db:test:load 不会执行

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)

转身以后 2024-08-14 17:30:09

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

使用 Minitest

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

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

  def setup
    Rails.application.load_seed
  end

  # tests here...

end

使用 RSpec

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

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

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

希望这有帮助。

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

Using 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:

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

  def setup
    Rails.application.load_seed
  end

  # tests here...

end

Using RSpec

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

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

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

Hope this helps.

好多鱼好多余 2024-08-14 17:30:09

将类似的内容放入 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

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

namespace :db do
  namespace :test do
    task :load => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end
琉璃梦幻 2024-08-14 17:30:09

基于马特的回答,如果采取这种路线,我建议在 rspec_helper.rb< 的 before(:suite) 块中调用 Rails.application.load_seed /code> 而不是在任何文件的 before(:all) 块中。这样,整个测试套件仅调用种子代码一次,而不是每组测试调用一次。

rspec_helper.rb:

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

Building on Matt's answer, if taking that sort of route, I recommend calling Rails.application.load_seed in a before(:suite) block in rspec_helper.rb rather 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.

rspec_helper.rb:

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

只想待在家 2024-08-14 17:30:09

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

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

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

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

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

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

末が日狂欢 2024-08-14 17:30:09

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

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

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

Adding Rake::Task["db:seed"].invoke to the db:test:prepare rake 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.

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

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

追我者格杀勿论 2024-08-14 17:30:09

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

只需将 Rake::Task['db:seed'].invoke 放入 test_helper 就会导致:

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

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

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

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.

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

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

But when we added load_tasks before that, it worked:

MyApp::Application.load_tasks
Rake::Task['db:seed'].invoke
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文