如何在Rails Capybara测试中为几个“场景”运行一次安装程序

时间:2013-11-05 20:55:46

标签: ruby-on-rails rspec capybara rspec2 poltergeist

与问题相关:how to run capybara commands once, then run some tests

如何设置一个Capybara测试,只运行一次背景,并在那里为测试中的场景记录数据库。

注意,我在测试之间使用database_cleaner。是的,我有一些seed_tables,但我希望有一个不是全局的范围,而只是一个集成测试文件。

feature "some feature" do
background do
 # set up some records that don't change between scenarios

 # set up some records that do change between scenarios
end

scenario "scenario 1" do
  # run tests
end

scenario "scenario 2" do
  # run tests
end
end

这是我对database_cleaner.rb的设置:

RSpec.configure do |config|
  config.add_setting(:seed_tables)
  config.seed_tables = %w(global_options shoot_types)

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation, except: config.seed_tables)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation, {except: config.seed_tables}
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

1 个答案:

答案 0 :(得分:4)

您可能知道background是功能测试before的别名(请参阅this)。所以,你可以使用:

background(:all) do
  # set up some records that don't change between scenarios
end

background(:each) do # or background
  # set up some records that do change between scenarios
end