Permalink
Browse files

Merge pull request #232 from urbanautomaton/optional-cleaner-hook

Allow users to disable database_cleaner hooks
  • Loading branch information...
2 parents ac9d843 + dbe2091 commit 7c57290c580ebf16edf8f209c2f9ec013e628ca5 @Kosmas Kosmas committed Mar 4, 2013
View
55 features/disable_automatic_database_cleaning.feature
@@ -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)
+ """
View
3 lib/cucumber/rails/database.rb
@@ -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
View
4 lib/cucumber/rails/hooks/database_cleaner.rb
@@ -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 7c57290

Please sign in to comment.