Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell cucumber & databasecleaner to leave my seed data in my rails test db?

I have several tables that contains parameters and other stuffs , it never changes !

But i cannot tell to database_cleaner to keep this data, after each test it truncate all my database.

Gems

gem "cucumber-rails", "~> 1.3.0", require: false
gem 'factory_girl_rails'
gem 'database_cleaner'

Config/cucumber.yml

enter code here
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'}     features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip

Features/support/env

require 'cucumber/rails'
require 'database_cleaner'
require 'database_cleaner/cucumber'
require 'factory_girl_rails'

Capybara.default_selector = :css
ActionController::Base.allow_rescue = false

begin
  DatabaseCleaner.logger = Rails.logger
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

Cucumber::Rails::World.use_transactional_fixtures = false
Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
end

Before do
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
  DatabaseCleaner.clean
end

Have you some clues ? Thanks, Nelson

like image 375
Nelson Avatar asked Nov 13 '22 04:11

Nelson


1 Answers

This question kept coming-up on search for 'database-cleaner cucumber setup truncate seed' so, even though it is an old question, I will add this answer.

For me, what worked, was to add a ruby file to the features/support directory with the following content:

Cucumber::Rails::Database.javascript_strategy = :truncation, {:except => %w[regions departements villes]}

(I have substituted your table names for mine.) You could add this line to env.rb, but that file has a warning not to edit. Apparently any .rb file in the features/support directory will run when you run cucumber. Mine is 'setup.rb'.

This overrides a default setting of Cucumber that truncates the entire database after an @javascript tagged scenario.

Versions in use: cucumber (1.3.9), cucumber-rails (1.4.0).

See also How do I get cucumber and databasecleaner to leave my seed data in my rails test db? and try

DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes]}

See http://github.com/DatabaseCleaner/database_cleaner#how-to-use in the docs. Repetition aside, it appears you were already doing that, as was I. Setting Cucumber::Rails::Database.javascript_strategy was the extra sauce that fixed up my javascript tests on the Selenium driver.

Now wash, and make it DRY ;)

like image 141
Douglas Lovell Avatar answered Dec 30 '22 07:12

Douglas Lovell