Want to load seed data before running cucumber
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 开发者_StackOverflow中文版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!
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...
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
精彩评论