How to use DatabaseCleaner [: active_record] .strategy =: selenium transaction

I wrote cucumber test cases and now I need to rollback the database and not the existing one. I am using selenium web driver with capybara 2.0.2. When I tried with:

DatabaseCleaner[:active_record].strategy = :truncation

      

It deletes all records of my mysql table. I subsequently changed this:

 DatabaseCleaner[:active_record].strategy = :transaction

      

But this is not the current database.

My .rb database:

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 it also does not rollback databases. Note. I only want to rollback the last transaction that is being executed by the test cases.

sorry for my English

+3


source to share


1 answer


The database_cleaner github page says to put this code in your file features/support/database_cleaner.rb

:



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

      

0


source







All Articles