ruby-on-rails - 在 Cucumber 中的每一步之前自动切换 PostgreSQL 架构 - "BeforeStep"?

标签 ruby-on-rails postgresql cucumber

我正在尝试使用 Cucumber 测试 Multi-Tenancy PostgreSQL 应用程序。似乎每一步都重置数据库连接,因此需要在每一步设置schema_search_path。我知道钩子(Hook),并寻找 BeforeStep,但是 there's none .该帖子提到使用 BeforeAfterStep 的组合,但这也不起作用,因为 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/

相关文章:

ruby-on-rails - Rails 哈希键/值在本地与 heroku 中的创建方式不同

ruby-on-rails - 如何在 Windows IIS 7 上重新启动 Ruby on Rails?

ruby-on-rails - Capistrano:部署权限问题

postgresql - 如何在 postgresql 中创建和存储对象数组

linux - [unixODBC][驱动程序管理器]无法打开 lib '/usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so' : file not found

java - 如何在 scene.write() 中写入 REST Assured 日志?

testing - 在步骤定义类中包含非步骤方法是否可以接受?

ruby-on-rails - Ruby on Rails 中的多个用户角色

python - 将 Python 连接到 Heroku PostgreSQL 数据库?

ruby-on-rails - 计算表格中的元素