我正在尝试使用 Cucumber 测试 Multi-Tenancy PostgreSQL 应用程序。似乎每一步都重置数据库连接,因此需要在每一步设置schema_search_path
。我知道钩子(Hook),并寻找 BeforeStep
,但是 there's none .该帖子提到使用 Before
和 AfterStep
的组合,但这也不起作用,因为 schema_search_path
在每一步之前都被重置。
如何在每一步之前自动和 DRYly 切换数据库,以便我的 Cuke 世界与应用程序 session 一致?
想看详情?这是我现在的设置(注意:我正在使用 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
下面是该步骤的 cucumber 输出:
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
最佳答案
您的应用程序如何切换模式?您的 Cucumber 步骤应该以相同的方式执行(可能通过应用程序 UI)。
为什么要为模式烦恼呢? Multi-Tenancy 应用程序通常应将所有数据存储在一个模式中:如果将数据存储在一起,则比将数据分开存储时将数据粘合在一起更容易。
关于ruby-on-rails - 在 Cucumber 中的每一步之前自动切换 PostgreSQL 架构 - "BeforeStep"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283649/