3

I have written cucumber test cases and now i need to rollback database not the existing one. I have use selenium web-driver with capybara 2.0.2. When i have tried with:

DatabaseCleaner[:active_record].strategy = :truncation

It is deleted all records of my mysql table. Afterwards i have changed this is by:

 DatabaseCleaner[:active_record].strategy = :transaction

But this is not rolling database.

My database.rb is:

require 'active_record'
require 'database_cleaner'
require 'database_cleaner/cucumber'

ActiveRecord::Base.establish_connection(
    :adapter => 'mysql2',
    :database => 'aq_test',
    :username => 'root',
    :password => 'manager'  )

class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

    def self.connection
        @@shared_connection || retrieve_connection
    end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
DatabaseCleaner[:active_record].strategy = :transaction

But this is also not roll back the databases Note: I want only to rollback last transaction which is run by test cases

Sorry for my english

vijay chouhan
  • 1,012
  • 8
  • 25

1 Answers1

0

The database_cleaner github page says to put that code into your features/support/database_cleaner.rb file:

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
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end
claptimes
  • 1,615
  • 15
  • 20
  • I need to use DatabaseCleaner.strategy = :transaction with selenium. Because of I want rollback not delete all data. Add any other suggestions. – vijay chouhan Feb 04 '13 at 13:48
  • Sorry, I don't think you can do that with Selenium. Here's a [stackoverflow](http://stackoverflow.com/questions/7511520/database-cleaner-transaction-with-cucumber-rails) answer and Github [blog](http://weilu.github.com/blog/2012/11/10/conditionally-switching-off-transactional-fixtures/) – claptimes Feb 04 '13 at 14:02
  • It is possible to specify tables that should not be deleted both with truncation and deletion. – Kristiina Sep 09 '13 at 09:48