如何自动将 db:seed 数据加载到测试数据库中?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
db:seed
rake 任务主要只是加载db/seeds.rb
脚本。因此只需执行该文件即可加载数据。将其放置在哪里取决于您使用的测试框架以及您是否希望在每次测试之前加载它还是仅在开始时加载一次。您可以将其放入
setup
调用或test_helper.rb
文件中。The
db:seed
rake task primarily just loads thedb/seeds.rb
script. Therefore just execute that file to load the data.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 atest_helper.rb
file.我想说应该是
因为如果你有 config.active_record.schema_format = :sql (db:test:clone_struct 是),则 db:test:load 不会执行
I'd say it should be
Because db:test:load is not executed if you have config.active_record.schema_format = :sql (db:test:clone_structure is)
我相信上面的史蒂夫的评论应该是正确答案。您可以使用 Rails.application.load_seed 将种子数据加载到测试环境中。但是,加载此数据的时间和频率取决于以下几点:
使用 Minitest
在所有测试之前没有方便的方法运行此文件一次(请参阅 此 Github 问题)。您需要在每次测试之前加载一次数据,可能在测试文件的设置方法中:
使用 RSpec
使用 RSpec 的
before(:all)
方法加载该模型所有测试的种子数据:希望这有帮助。
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:
Using RSpec
Use RSpec's
before(:all)
method to load seed data for all test for this model:Hope this helps.
将类似的内容放入 lib/tasks/test_seed.rake 应该在 db:test:load 之后调用种子任务:
Putting something like this in lib/tasks/test_seed.rake should invoke the seed task after db:test:load:
基于马特的回答,如果采取这种路线,我建议在
rspec_helper.rb< 的
before(:suite)
块中调用Rails.application.load_seed
/code> 而不是在任何文件的before(:all)
块中。这样,整个测试套件仅调用种子代码一次,而不是每组测试调用一次。rspec_helper.rb:
Building on Matt's answer, if taking that sort of route, I recommend calling
Rails.application.load_seed
in abefore(:suite)
block inrspec_helper.rb
rather than in abefore(: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:
我们将调用 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.
将
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 thedb:test:prepare
rake task did not work for me. If I prepared the database withrake 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!
对于那些使用种子库的人来说,它会改变种子的加载方式,因此您可能不能/不想使用此处提供的
load ...
解决方案。只需将
Rake::Task['db:seed'].invoke
放入 test_helper 就会导致:但是当我们在此之前添加 load_tasks 时,它起作用了:
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:But when we added load_tasks before that, it worked: