我写了黄瓜测试用例,现在我需要回滚数据库而不是现有数据库。我使用了seyium 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
但这也不会回滚数据库 注意:我只想回滚由测试用例运行的最后一个事务
抱歉我的英文
答案 0 :(得分: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