mahu
mahu

Reputation: 297

Run exec 'rake db:drop db:create db:migrate db:seed' in cucumber before each scenario

I searched and tried a lot, but did not find my answer. I have some feature files for my project written in gherkin (in a rails project). All steps are defined and the features themselves run pretty good if I start them each single. That I can run all scenarios together with cucumber I need to perform rake db:drop db:create db:migrate db:seed before each scenario. I tried exec 'rake db:drop db:create db:migrate db:seed' in a begin function in the env.rb and I tried the same in a backgound step in the features. The command is executed but after that cucumber stops and the scenarios are not executed. How can I use this so?

Thanks for your help!

Upvotes: 2

Views: 517

Answers (2)

mahu
mahu

Reputation: 297

Didn't understand database_cleaner at first, but now I do.... there was the problem for seeding the "cleaned" DB. What I did now is (in the env.rb)

begin
    require 'database_cleaner'
    require 'database_cleaner/cucumber'
    DatabaseCleaner.strategy = :truncation
  rescue NameError
    raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Before do |scenario|
    load Rails.root.join('db/seeds.rb')
end
Around do |scenario, block|
    DatabaseCleaner.cleaning(&block)
end

Upvotes: 1

irnmn
irnmn

Reputation: 736

Have you looked into the Database Cleaner gem? It might help you out with this.

Upvotes: 0

Related Questions