Permalink
Browse files

Allow users to disable database_cleaner hooks

The hooks that automatically invoke database_cleaner before every
scenario can sometimes cause problems when switching cleaning strategies
between scenarios (issue #215).

To allow a user to take full control of when database_cleaner is
invoked, the configuration option
Cucumber::Rails::Database.autorun_database_cleaner is provided,
defaulting to true.
  • Loading branch information...
Simon Coffey
Simon Coffey committed Feb 2, 2013
1 parent f16e1d2 commit dbe2091a7c38554d5b875a4722937b27cbef4770
@@ -0,0 +1,55 @@
+Feature: Disable automatic database cleaning
+
+ By default, a set of Before/After hooks are installed to
+ invoke database_cleaner on every scenario except those tagged
+ "@no-database-cleaner". Sometimes when a user is switching between
+ cleaning strategies, this can initiate an undesired database
+ transaction.
+
+ To avoid the need for users making frequent strategy switches to apply
+ this tag, a configuration option is provided so that the user can
+ control the invocation of database_cleaner explicitly.
+
+ Scenario: Disabling automatic cleaning
+ Given I have created a new Rails 3 app and installed cucumber-rails
+ And I append to "features/env.rb" with:
+ """
+ Cucumber::Rails::Database.autorun_database_cleaner = false
+ """
+ And I write to "features/widgets.feature" with:
+ """
+ Feature: Create widgets
+ Scenario: Create 3 widgets
+ When I create 3 widgets
+ Then I should have 3 widgets
+
+ Scenario: Create 5 widgets
+ When I create 5 widgets
+ Then I should have 8 widgets
+ """
+ And I successfully run `rails generate model widget name:string`
+ And I write to "features/step_definitions/widget_steps.rb" with:
+ """
+ Given /^I have (\d+) widgets$/ do |n|
+ n.to_i.times do |i|
+ Widget.create! :name => "Widget #{Widget.count + i}"
+ end
+ end
+
+ When /^I create (\d+) widgets$/ do |n|
+ n.to_i.times do |i|
+ Widget.create! :name => "Widget #{Widget.count + i}"
+ end
+ end
+
+ Then /^I should have (\d+) widgets$/ do |n|
+ Widget.count.should == n.to_i
+ end
+ """
+ And I run `bundle exec rake db:migrate`
+ And I run `bundle exec rake cucumber`
+ Then it should pass with:
+ """
+ 2 scenarios (2 passed)
+ 4 steps (4 passed)
+ """
@@ -8,6 +8,8 @@ class InvalidStrategy < ArgumentError;end
class << self
+ attr_accessor :autorun_database_cleaner
+
def javascript_strategy=(args)
strategy, *strategy_opts = args
strategy_type =
@@ -108,6 +110,7 @@ def before_non_js
end
Database.javascript_strategy = :truncation
+ Database.autorun_database_cleaner = true
end
end
end
@@ -2,11 +2,11 @@
require 'database_cleaner'
Before('~@no-database-cleaner') do
- DatabaseCleaner.start
+ DatabaseCleaner.start if Cucumber::Rails::Database.autorun_database_cleaner
end
After('~@no-database-cleaner') do
- DatabaseCleaner.clean
+ DatabaseCleaner.clean if Cucumber::Rails::Database.autorun_database_cleaner
end
rescue LoadError => ignore_if_database_cleaner_not_present

0 comments on commit dbe2091

Please sign in to comment.