在Cucumber的每一步 - “BeforeStep”之前自动切换PostgreSQL模式?

时间:2011-11-27 04:28:14

标签: ruby-on-rails postgresql cucumber

我正在尝试使用Cucumber测试多租户PostgreSQL应用程序。似乎每个步骤都重置数据库连接,因此需要在每个步骤中设置schema_search_path。我知道钩子,并寻找BeforeStep,但there's none。该帖子提到使用BeforeAfterStep的组合,但这也不起作用,因为schema_search_path在每个步骤之前都会重置。

如何在everystep之前自动和DRYly切换数据库,以便我的Cuke世界与应用程序会话保持一致?

想看详情?这是我现在的设置(注意:我正在使用Apartment gem进行切换):

Before do
  # ...
  Apartment::Database.switch @current_site.subdomain if @current_site
  # ...
end

AfterStep do
  puts "start of AfterStep. current_database: #{Apartment::Database.current_database}"
  # ...
  Apartment::Database.switch @current_site.subdomain if @current_site
  # ...
  puts "end of AfterStep. current_database: #{Apartment::Database.current_database}"
end

然后我有一些步骤:

Given /^a site "([^"]*)" exists$/ do |site_name|
  @current_site = Site.make! :name => site_name, :subdomain => site_name.downcase.underscore
  Apartment::Database.switch @current_site.subdomain
end

When /^I upload an image to the image field$/ do
  image_path = "#{Rails.root}/spec/fixtures/5x5.jpg"
  puts "About to attach_file. that's controlled by selenium so the ApplicationController should switch for us."
  attach_file "File", image_path
  puts "current site: #{@current_site.subdomain}"
  puts "In the schema #{Apartment::Database.current_database}"
  sleep 2
  puts "Here are all images in #{Apartment::Database.current_database}: #{Image.all.inspect}"
end

以下是黄瓜输出在这一步中的表现:

Given the site "Fancake" exists
# Other steps...
When I upload an image to the image field
  About to attach_file. that's controlled by selenium so the ApplicationController should switch for us.
  current site: fancake
  In the schema "$user",public
  Here are all images in "$user",public: []
  start of AfterStep. current_database: "$user",public
  end of AfterStep. current_database: fancake

1 个答案:

答案 0 :(得分:1)

您的应用程序如何切换架构?您的Cucumber步骤应该以相同的方式执行(可能通过应用程序UI)。

为什么要使用架构呢?多租户应用程序通常应将所有数据存储在一个模式中:如果数据存储在一起,则更容易将数据分解,而不是将数据粘合在一起(如果它们存储在一起)。