如何辨别黄瓜和databasecleaner将我的种子数据留在我的rails测试数据库中?

时间:2012-05-25 22:19:08

标签: database ruby-on-rails-3 rspec cucumber

我有几个包含参数和其他内容的表,它永远不会改变!

但我无法告诉database_cleaner保留这些数据,每次测试后它都会截断我的所有数据库。

宝石

gem "cucumber-rails", "~> 1.3.0", require: false
gem 'factory_girl_rails'
gem 'database_cleaner'

配置/ cucumber.yml

enter code here
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'}     features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip

功能性/支撑性/ env的

require 'cucumber/rails'
require 'database_cleaner'
require 'database_cleaner/cucumber'
require 'factory_girl_rails'

Capybara.default_selector = :css
ActionController::Base.allow_rescue = false

begin
  DatabaseCleaner.logger = Rails.logger
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

Cucumber::Rails::World.use_transactional_fixtures = false
Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
end

Before do
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean_with(:truncation, :except => %w[regions departements villes positions levels footballtypes genders publicies types])
  DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}
  DatabaseCleaner.clean
end

你有一些线索吗? 谢谢,尼尔森

2 个答案:

答案 0 :(得分:1)

这个问题一直在寻找'数据库清洁黄瓜设置截断种子',所以即使这是一个老问题,我也会添加这个答案。

对我来说,有用的是将ruby文件添加到features / support目录中,其中包含以下内容:

Cucumber::Rails::Database.javascript_strategy = :truncation, {:except => %w[regions departements villes]}

(我已经替换了我的表名。)您可以将此行添加到env.rb,但该文件有一个警告,不能编辑。显然,在运行黄瓜时,feature / support目录中的任何.rb文件都会运行。我的是'setup.rb'。

这会覆盖Cucumber的默认设置,该设置会在@javascript标记的方案后截断整个数据库。

使用中的版本:黄瓜(1.3.9),黄瓜 - 铁轨(1.4.0)。

另请参阅How do I get cucumber and databasecleaner to leave my seed data in my rails test db?并尝试

DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes]}

请参阅文档中的http://github.com/DatabaseCleaner/database_cleaner#how-to-use。除了重复之外,看起来你已经这样做了,就像我一样。设置Cucumber::Rails::Database.javascript_strategy是在Selenium驱动程序上修复我的javascript测试的额外酱。

现在洗,然后擦干;)

答案 1 :(得分:0)

上面粘贴的代码(env.rb)令人不安的第一件事就是重复。使用database_cleaner时,我从来没有这样做过。如果您将文件简化为此会发生什么:

require 'cucumber/rails'
require 'database_cleaner'
require 'database_cleaner/cucumber'
require 'factory_girl_rails'

Capybara.default_selector = :css
ActionController::Base.allow_rescue = false
Cucumber::Rails::World.use_transactional_fixtures = false


DatabaseCleaner.logger = Rails.logger
DatabaseCleaner.strategy = :truncation, {:except => %w[regions departements villes positions levels footballtypes genders publicies types]}

Before do
  DatabaseCleaner.start
end

After do |scenario|
  DatabaseCleaner.clean
end

这与我过去所做的和我所看到的工作更接近。