Want to load seed data before running cucumber

淺唱寂寞╮ 提交于 2020-01-14 02:57:08

问题


I want cucumber to load my seed data in "db/seeds.rb" before starting to test. Not before each scenario or feature, but only once before running the tests. And also after each scenario, the seeds must remain in db.

Is that possible?

I've tried creating a file "features/support/seed_data.rb" and requiring my db/seeds.rb in there, but it seems that file is not loaded at all. I tried to require my seeds in env.rb - no affect.

Please, can anybody suggest me the solution?

Thanks in advance!


回答1:


Create a before hook in your support/hooks.rb file which looks like this:

Before('@load-seed-data') do
  load File.join(Rails.root, 'db', 'seeds.rb')
end

In your test before the scenario, call the hook like this:

@load-seed-data @US49
Scenario: This is a scenario that needs seed data.
Given...



回答2:


How about pulling the code from your seeds.rb file and sticking it in hooks.rb in an AfterConfiguration block?

AfterConfiguration do |config|
  # Your code from seeds.rb
end

That should be called once during a run, right after cucumber is configured. At least doing it this way, you can determine if you just have an issue with including your seeds file or not. Another idea would be to take seeds.rb and stick it directly inside the support directory as a module and then call it from AfterConfiguration:

# db_seeds.rb
module DbSeeds 

  def seed_db
    # Your Code
  end

end

World(DbSeeds)

#hooks.rb
AfterConfiguration do |config|
  seed_db
end


来源:https://stackoverflow.com/questions/5296270/want-to-load-seed-data-before-running-cucumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!