2

I set up cucumber/rails for my rails project and populated the test database with data, to run tests against it. When I run "rake cucumber" the database gets truncated. I tried to set DatabaseCleaner.strategy to :transaction and nil, but it still gets truncated. I'd prefer not to use database_cleaner at all for now, but its presence is required by cucumber. Here is my "./features/support/env.rb" file:

require 'cucumber/rails'
require 'capybara/cucumber'

Capybara.default_driver = :selenium

ActionController::Base.allow_rescue = false


begin
  DatabaseCleaner.strategy = nil
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::Database.javascript_strategy = :truncation

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
naushniki
  • 21
  • 3

3 Answers3

3

I don't believe there is a strategy that does nothing, but cucumber doesn't need it to run. Possibly you may have to remove it from your env.rb file and any db cleaning in your hooks file.

EDIT:

I was wrong, there is a null strategy. Try:

DatabaseCleaner.strategy = DatabaseCleaner::NullStrategy

or

DatabaseCleaner::Base.new

jmccure
  • 1,180
  • 7
  • 16
  • 1
    I tried removing DatabaseCleaner.strategy from ebv.rb, but this didn't change anything. The database_cleaner documentation suggests to set DatabaseCleaner.strategy to nil: [Database Cleaner also includes a null strategy (that does no cleaning at all) which can be used with any ORM library. You can also explicitly use it by setting your strategy to nil.](https://github.com/DatabaseCleaner/database_cleaner) For some reason this doesn't work for me. – naushniki Feb 16 '15 at 15:33
  • You are correct, there is a null strategy. Try: DatabaseCleaner.strategy = DatabaseCleaner::NullStrategy - I updated my answer – jmccure Feb 17 '15 at 11:27
1

You need to set both the DatabaseCleaner.strategy and the Cucumber::Rails::Database.javascript_strategy.

Cucumber-rails does not come with a null strategy so you have to make one. This works for me, in env.rb:

DatabaseCleaner.strategy = DatabaseCleaner::NullStrategy

class NullStrategy < Cucumber::Rails::Database::Strategy
  def before_js
    super DatabaseCleaner::NullStrategy
  end
end

Cucumber::Rails::Database.javascript_strategy = NullStrategy
Michael Johnston
  • 5,298
  • 1
  • 29
  • 37
  • +1 as your answer pointed me at inner details that I needed to understand. See my answer for a simpler approach for op's use case. – akostadinov Sep 23 '21 at 11:18
0

I believe this is the simplest and documented approach.

Cucumber::Rails::Database.autorun_database_cleaner = false
akostadinov
  • 17,364
  • 6
  • 77
  • 85