Ruby-on-rails 使用 RSpec,如何在加载时为数据库设置种子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6004057/
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-03 01:08:33  来源:igfitidea点击:

With RSpec, how to seed the database on load?

ruby-on-railsruby-on-rails-3rspec

提问by AnApprentice

I'm using rspec for testing w my rails 3 app. I need to seed the database before the tests start. How can I seed the database with the following:

我正在使用 rspec 来测试我的 rails 3 应用程序。我需要在测试开始之前为数据库做种。如何使用以下内容为数据库设定种子:

/db/seeds.rb

/db/seeds.rb

["Admin", "Member"].each do |role_name|
  Role.find_or_create_by_name(role_name)
end

Thanks

谢谢

采纳答案by Scott

Try, something like this

试试,像这样

rake db:seed RAILS_ENV=test

You can get a list of all rake commands doing

您可以获得所有 rake 命令的列表

rake -T

If this is test data, you may want to look at putting it into fixtures which will be loaded on the start of the tests.

如果这是测试数据,您可能希望将其放入将在测试开始时加载的夹具中。

回答by Hannes

In spec_helper.rb or rails_helper.rb:

在 spec_helper.rb 或 rails_helper.rb 中:

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

回答by Micha? Czapko

However Scott's solution surely works for you, I believe the better way to solve your problem was to put the code responsible for seeding your test database within RSpec's configure block:

然而,Scott 的解决方案肯定对您有用,我相信解决您的问题的更好方法是将负责为您的测试数据库播种的代码放在 RSpec 的配置块中:

I use SeedFu and in my spec_helper I have:

我使用 SeedFu 并且在我的 spec_helper 中我有:

RSpec.configure do |config|

  # some other configuration code ...

  config.before(:suite) do
    # ...
    SeedFu.seed
    # ...
  end

  # some other configuration code ...

end

回答by fearless_fool

I've followed the raging debate over at Auto-load the seed data from db/seeds.rb with rake. Die-hards maintain that you should never load seed data for tests, but I take the more moderate stance that there are occasions where you might want to load seed data for specific tests, e.g. verifying that the seed data exists.

我关注了在Auto-load the seed data from db/seeds.rb with rake 上的激烈争论。顽固派坚持认为永远不应该为测试加载种子数据,但我采取更温和的立场,即在某些情况下您可能希望为特定测试加载种子数据,例如验证种子数据是否存在。

Unlike some answers given here, I do notrecommend unconditionally loading the seeds from inside your spec_helper file. Instead, you can load your seeds using before :eachor before :allinside just those test files that need the seeds, e.g.:

与此处给出的某些答案不同,我建议无条件地从您的 spec_helper 文件中加载种子。相反,您可以使用before :eachbefore :all仅在需要种子的那些测试文件中加载您的种子,例如:

describe "db seed tests" do
  before(:each) do
    load "#{Rails.root}/db/seeds.rb" 
  end

  ...your test code here...
end

update

更新

As @marzapower points out, if you go this route, your seeds.db file should clear each table before creating entries or use find_or_create_bymethods. (Hint: the former is faster and more reliable.) This will prevent duplicate entries if you load the seeds.db file more than once.

正如@marzapower 指出的那样,如果你走这条路,你的seeds.db 文件应该在创建条目或使用find_or_create_by方法之前清除每个表。(提示:前者更快,更可靠。)如果您多次加载seeds.db 文件,这将防止重复条目。

回答by Ahmad Hussain

To load seeds in rspec you need to add it after database cleanup in confg.before(:suite) in spec_helper

要在 rspec 中加载种子,您​​需要在 spec_helper 的 confg.before(:suite) 中的数据库清理后添加它

config.before(:suite) do
  DatabaseCleaner.clean_with(:truncation)
  load Rails.root.join('db', 'seeds.rb')
end

回答by Karen

I ended up needing to use DatabaseCleaner to truncate the database, then load the rake task that does my seeding (because I use seedbank). After that, I wound up wrapping my tests in a transaction like on the database_cleaner README, so that each test could run with a freshly loaded site.

我最终需要使用 DatabaseCleaner 来截断数据库,然后加载进行播种的 rake 任务(因为我使用的是seedbank)。在那之后,我最终将我的测试包装在一个事务中,比如database_cleaner README,这样每个测试都可以在新加载的站点上运行。

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    MyApplicationName::Application.load_tasks
    Rake::Task['db:seed'].invoke # loading seeds
  end
  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end