如何使用rake db:seed将我的种子数据添加到我的测试数据库?

时间:2022-02-26 16:46:01

I'm using Factory Girl to populate my seed data and adding it to the db in seed.rb.

我正在使用Factory Girl填充种子数据并将其添加到seed.rb中的db。

I'm then running my tests using Cucumber.

我正在使用Cucumber运行我的测试。

I have a price table that contains seed data that I want in all my environments.

我有一个价格表,其中包含我想要在所有环境中使用的种子数据。

I want rake db:seed to add it to my dev and test db's and for cucumber to be able to use that test seed. Then I'll add that seed data in prod also.

我想rake db:seed将它添加到我的dev并测试db和黄瓜能够使用该测试种子。然后我也会在prod中添加种子数据。

How can I ensure that my seed data is added to both dev and test db's?

如何确保将我的种子数据添加到dev和test db中?

rake db:seed #only adds it only to my dev database

3 个解决方案

#1


31  

You can try something like this:

你可以尝试这样的事情:

rake db:seed RAILS_ENV=test --trace
rake db:seed RAILS_ENV=production --trace

#2


8  

Check out this answer from a similiar post.

从类似的帖子中查看此答案。

I really think it's better to use factories to fill test database. And if you need seed data during your tests add it as a before :all block in spec_helper/test_helper.

我真的认为使用工厂来填充测试数据库会更好。如果您在测试期间需要种子数据,请将其添加为之前:spec_helper / test_helper中的所有块。

#3


0  

You may want to visit this thread as well when working with a test database, especially since you do not want to persist data in your test database.

在使用测试数据库时,您可能也想访问此线程,尤其是因为您不希望在测试数据库中保留数据。

As your tests start to evolve you will most likely find yourself using the seed data, factories, as well as mocks, and with persistent data you will find yourself running into conflicts.

随着测试开始发展,您很可能会发现自己使用种子数据,工厂以及模拟,并且使用持久性数据,您会发现自己遇到了冲突。

Personally I like to use a custom seed file for my test database, and load it in my rails_helper:

我个人喜欢为我的测试数据库使用自定义种子文件,并将其加载到我的rails_helper中:

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

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

#1


31  

You can try something like this:

你可以尝试这样的事情:

rake db:seed RAILS_ENV=test --trace
rake db:seed RAILS_ENV=production --trace

#2


8  

Check out this answer from a similiar post.

从类似的帖子中查看此答案。

I really think it's better to use factories to fill test database. And if you need seed data during your tests add it as a before :all block in spec_helper/test_helper.

我真的认为使用工厂来填充测试数据库会更好。如果您在测试期间需要种子数据,请将其添加为之前:spec_helper / test_helper中的所有块。

#3


0  

You may want to visit this thread as well when working with a test database, especially since you do not want to persist data in your test database.

在使用测试数据库时,您可能也想访问此线程,尤其是因为您不希望在测试数据库中保留数据。

As your tests start to evolve you will most likely find yourself using the seed data, factories, as well as mocks, and with persistent data you will find yourself running into conflicts.

随着测试开始发展,您很可能会发现自己使用种子数据,工厂以及模拟,并且使用持久性数据,您会发现自己遇到了冲突。

Personally I like to use a custom seed file for my test database, and load it in my rails_helper:

我个人喜欢为我的测试数据库使用自定义种子文件,并将其加载到我的rails_helper中:

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

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