3

我已经编写了黄瓜测试用例,现在我需要回滚数据库而不是现有数据库。我已经使用 selenium web-driver 和 capybara 2.0.2。当我尝试过:

DatabaseCleaner[:active_record].strategy = :truncation

它被删除了我的mysql表的所有记录。后来我改变了这是通过:

 DatabaseCleaner[:active_record].strategy = :transaction

但这不是滚动数据库。

我的 database.rb 是:

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

但这也不是回滚数据库注意:我只想回滚由测试用例运行的最后一个事务

对不起我的英语不好

4

1 回答 1

0

database_cleaner github页面说将该代码放入您的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
于 2013-02-04T13:36:44.663 回答