5

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

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

宝石

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

配置/黄瓜.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

功能/支持/环境

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

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

4

2 回答 2

1

这个问题在搜索“数据库清洁黄瓜设置截断种子”时不断出现,所以即使这是一个老问题,我也会添加这个答案。

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

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

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

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

正在使用的版本:黄瓜(1.3.9),黄瓜轨(1.4.0)。

另请参阅如何让 cucumber 和 databasecleaner 将种子数据留在我的 rails 测试数据库中?并尝试

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

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

现在洗,并使其干燥;)

于 2013-11-08T21:39:43.193 回答
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

这更接近于我过去所做的以及我所看到的工作。

于 2012-05-26T14:05:53.533 回答